Google had released a stable version of the lifecycle library. Here is something you need to know if you are still using the alpha or beta version.

1. No More LifecycleActivity

LifecycleActivity is a subclass of FragmentActivity, and therefore the lifecycle library does not support AppCompativity and Fragment back then.

But LifecycleActivity is deprecated in the stable 1.0.0 version. Now you should use AppCompatActivity AppCompatActivity implements the LifeOwner interface now, and it can replace the old LifecycleActivity.

2. AppCompatActivity is good for you?

I did what the google suggests, and replace the LifecycleActivity with AppCompatActivity. But the getLifecycle() method does not exist:

You see, the getLifecycle() turns to red, which means Android Studio spot a compile error.

Then I realized there must be the version issue. The AppCompatActivity I used before is from version 25.3.1. So I tried To update the version from 25.3.1 to 25.4.0, 26.0.0, 26.0.1, 26.0.2, But the getLifecycle() method is still missing in the AppCompatActivity. After I bump the version number to 26.1.0, the AppCompatActivity finally has the getLifecycle() method. What a relief!

So here is the conclusion. Not any AppCompatActivity has the functionality of lifecycle library. To use the latest Stable Lifecycle Library, you have to update the support: AppCompat-V7 to Version 26.1.0.

Implementation 'com. Android. Support: appcompat - v7:26.1.0'Copy the code

3. No More LifeRegistryOwner

I made a LifecycleAppCompatActivity in September to help me to support lifecycle library in the AppCompatActivity, just like the following code.

public class LifecycleAppCompatActivity extends AppCompatActivity implements LifecycleRegistryOwner { .. }Copy the code

But now the LifecycleRegistryOwner is alose deprecated. If you want to custom your own lifecycle owner, you should implements LifecycleOwner, rather than the LifecycleRegistryOwner.

4. Demo

So my previous LifecycleAppCompatActiivty is not useful anymore. You should use AppCompatActivity instead.

Here is the complete demo I made. I’ve used the lifecycle:1.0.0 library in this demo.

[build.gradle]

Implementation 'com. Android. Support: appcompat - v7:26.1.0' implementation "android. Arch. Lifecycle: the runtime: 1.0.3" AnnotationProcessor "android. Arch. Lifecycle: the compiler: 1.0.0"Copy the code

[Activity]

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Watcher03 watcher = new Watcher03(); getLifecycle().addObserver(watcher); }}Copy the code

[Observer]

public class Watcher03 implements LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_ANY) public void onAny(LifecycleOwner owner, Lifecycle.Event event) { System.out.println("szw watcher03 : onAny(" + event.name() + ")"); } @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) public void connectListener() { System.out.println("szw watcher03 : onResume()"); } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) public void disconnectListener() { System.out.println("szw watcher03 : onPause()"); }}Copy the code