Observer methods are not being called. I tested ViewPagerCycler
as in old school ways and working perfect. Thanks for help.
public final class ViewPagerCycler implements LifecycleObserver {
private static final int PERIOD = 3000;
private Timer mTimer;
private TimerTask mTask;
private Activity mActivity;
private ViewPager mPager;
public ViewPagerCycler(Activity activity, ViewPager pager) {
mActivity = activity;
mPager = pager;
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME) public void start() {
int count = mPager
.getAdapter()
.getCount();
mTimer = new Timer();
mTask = new TimerTask() {
@Override public void run() {
mActivity.runOnUiThread(new TimerTask() {
@Override public void run() {
mPager.setCurrentItem((mPager.getCurrentItem() + 1) % count, true);
}
});
}
};
mTimer.schedule(mTask, PERIOD, PERIOD);
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) public void stop() {
mTask.cancel();
mTimer.cancel();
mTimer.purge();
}
}
Activity is just summarized. I didn't add findViewById
s.
public class SummariziedActivity extends Activity implements LifecycleOwner {
private final LifecycleRegistry mRegistry = new LifecycleRegistry(this);
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mRegistry.addObserver(new ViewPagerCycler(mPager));
}
}
In the build.gradle
:
compile "android.arch.lifecycle:runtime:1.0.0-alpha2"
compile "android.arch.lifecycle:extensions:1.0.0-alpha2"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha2"
LifecycleOwner. LifecycleOwner is a single method interface that denotes that the class has a Lifecycle . It has one method, getLifecycle() , which must be implemented by the class. If you're trying to manage the lifecycle of a whole application process instead, see ProcessLifecycleOwner .
Lifecycle Awareness :Viewmodel is lifecycle aware. It is automatically cleared when the lifecycle they are observing gets permanently destroyed.
class NetworkMonitor @Inject constructor(private val context: Context) : LifecycleObserver { // Code to observe changes in the network connection. } That's it. Now, NetworkMonitor is a lifecycle observer component. You've completed the first step to convert it to a lifecycle-aware component.
Instead of extending ordinary Activity
, you should extend from LifecycleActivity
.
Otherwise, you can refer to "Implementing LifecycleOwner in custom activities and fragments":
Any custom fragment or activity can be turned into a LifecycleOwner by implementing the built-in LifecycleRegistryOwner interface (instead of extending LifecycleFragment or LifecycleActivity).
public class MyFragment extends Fragment implements LifecycleRegistryOwner {
LifecycleRegistry lifecycleRegistry = new LifecycleRegistry(this);
@Override
public LifecycleRegistry getLifecycle() {
return lifecycleRegistry;
}
}
If you have a custom class that you would like to make a LifecycleOwner, you can use the LifecycleRegistry class, but you will need to forward events into that class. This forwarding is done automatically for fragments and activities if they implement the LifecycleRegistryOwner interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With