Legacy Options
The Java 8 and FragmentActivity
approach is the simplest way to work with lifecycles. However, sometimes, those are not an option, and for that, you will need workarounds.
Ordinary Activities and Fragments, and Other Objects
Sometimes, you have to use activities and fragments not rooted in the Support Library backport. For example, WearableActivity
for Android Wear does not extend FragmentActivity
. By default, you cannot use such activities with the lifecycle system. And, sometimes, you might have some other object need to be the source of lifecycle events, independent of activities and fragments.
For these scenarios:
- Have your class implement the
LifecycleOwner
interface - Use
LifecycleRegistry
to track the registered observers - Return that registry from
getLifecycle()
, the one method onLifecycleOwner
that you need to implement - From all of the lifecycle methods, call
handleLifecycleEvent()
on the registry, indicating what lifecycle event has just occurred
For example, here is a SimpleLifecycleActivity
that handles the standard activity lifecycle events, forwarding them to the LifecycleRegistry
:
package com.commonsware.android.lifecycle;
import android.app.Activity;
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.LifecycleRegistry;
import android.os.Bundle;
import android.support.annotation.Nullable;
public class SimpleLifecycleActivity extends Activity
implements LifecycleOwner {
private LifecycleRegistry registry=new LifecycleRegistry(this);
@Override
public Lifecycle getLifecycle() {
return(registry);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
}
@Override
protected void onStart() {
super.onStart();
registry.handleLifecycleEvent(Lifecycle.Event.ON_START);
}
@Override
protected void onResume() {
super.onResume();
registry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
}
@Override
protected void onPause() {
super.onPause();
registry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE);
}
@Override
protected void onStop() {
super.onStop();
registry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
}
@Override
protected void onDestroy() {
super.onDestroy();
registry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
}
}
Pre-Java 8
Perhaps Java 8 is not an option for you, for whatever reason. DefaultLifecycleObserver
will not work for you. Instead, you will need to:
- Remove the
android.arch.lifecycle:common-java8
dependency, as it will not be compatible with your app - Add an
annotationProcessor
dependency onandroid.arch.lifecycle:compiler
- Have your observer implement
LifecycleObserver
instead ofDefaultLifecycleObserver
- Implement one or more methods, annotated with
@OnLifecycleEvent
, to receive the lifecycle events of interest to you
So, for example, here is an observer that passes all events to a RecyclerView.Adapter
named EventLogAdapter
:
static class LObserver implements LifecycleObserver {
private final EventLogAdapter adapter;
LObserver(EventLogAdapter adapter) {
this.adapter=adapter;
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
void created() {
adapter.add("ON_CREATE");
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
void started() {
adapter.add("ON_START");
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
void resumed() {
adapter.add("ON_RESUME");
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
void paused() {
adapter.add("ON_PAUSE");
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
void stopped() {
adapter.add("ON_STOP");
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
void destroyed() {
adapter.add("ON_DESTROY");
}
}
Note:
- There is also a
Lifecycle.Event.ON_ANY
event that you can request; this triggers your method to be called for any lifecycle event… though you have no way of knowing what event it was - A single method can only have one
@OnLifecycleEvent
annotation, and that annotation accepts only a singleLifecycle.Event
value (not a list)
As noted, you also need the annotation processor, so those @OnLifecycleEvent
annotations can be interpreted and applied:
dependencies {
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'android.arch.lifecycle:runtime:1.1.1'
annotationProcessor 'android.arch.lifecycle:compiler:1.1.0'
}
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.