Friday, 13 July 2018

How to make an Activitly correctly observe Lifecycle event

Currently, I need to perform some actions, when

  • Application is launched.
  • Application is quit.
  • But not during activity recreation, configuration change, ...

Hence, the following code snippet serve me pretty well so far. I learn such trick from CommonWare's - https://commonsware.com/AndroidArch/previews/other-lifecycle-owners and https://proandroiddev.com/react-to-app-foreground-and-background-events-with-processlifecycleowner-96278e5816fa

WeNoteApplication.java

public class WeNoteApplication extends Application {

    public static class AppLifecycleObserver implements DefaultLifecycleObserver {
        @Override
        public void onResume(LifecycleOwner owner) {
            // Do something when the application launched.
            // But not during activity recreation, configuration change, ...
        }

        @Override
        public void onPause(LifecycleOwner owner) {
            // Do something when the application quit.
            // But not during activity recreation, configuration change, ...
        }
    }

    private static final AppLifecycleObserver appLifecycleObserver = new AppLifecycleObserver();

    @Override
    public void onCreate() {
        super.onCreate();

        initLifecycleObserver();
    }

    private void initLifecycleObserver() {
        Lifecycle lifecycle = ProcessLifecycleOwner.get().getLifecycle();
        lifecycle.removeObserver(appLifecycleObserver);
        lifecycle.addObserver(appLifecycleObserver);
    }
}   


However, I also need to perform some actions, by using Activity, Fragment, ... For instance, showing a DialogFragment.

For my entry point main Activity, here's what I had tried.

public class MainActivity extends AppCompatActivity implements DefaultLifecycleObserver {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ProcessLifecycleOwner.get().getLifecycle().removeObserver(this);
        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);

        setContentView(R.layout.activity_main);
    }

    @Override
    public void onResume(LifecycleOwner owner) {
        android.util.Log.i("CHEOK", "onResume LifecycleOwner called");
    }

    @Override
    public void onPause(LifecycleOwner owner) {
        android.util.Log.i("CHEOK", "onPause LifecycleOwner called");
    }

    @Override
    public void onCreate(LifecycleOwner owner) {
        android.util.Log.i("CHEOK", "onCreate LifecycleOwner called");
    }
}

It doesn't work as expected due the following following observations

When the app is launched

onCreate LifecycleOwner called
onResume LifecycleOwner called
onResume LifecycleOwner called    <-- Why onResume of LifecycleOwner is called twice??

When I rotate the device

onCreate LifecycleOwner called
onResume LifecycleOwner called    <-- Why onCreate and onResume of LifecyclOwner is called during configuration change?


Try again with LiveData

I tried to use LiveData in order for AppLifecycleObserver to communicate with Activity. However, during configuration change, onResumeLiveData treats re-created Activity as new lifecycle owner. Hence, it will trigger it again.

public class WeNoteApplication extends Application {

    public MutableLiveData<LifecycleOwner> onResumeLiveData = new MutableLiveData<>();

    public class AppLifecycleObserver implements DefaultLifecycleObserver {
        @Override
        public void onResume(LifecycleOwner owner) {
            // This will only be called during app launch, not configuration change.
            android.util.Log.i("CHEOK", "onResume callback happen in application");
            onResumeLiveData.setValue(owner);
            ...


public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        WeNoteApplication.instance().onResumeLiveData.observe(this, new Observer<LifecycleOwner>() {
            @Override
            public void onChanged(@Nullable LifecycleOwner lifecycleOwner) {
                // This will only be called during app launch
                // This will also be called during configuration change.
                android.util.Log.i("CHEOK", "onResume callback happen in activity");
            }
        });

So, I'm some how confused. What is a correct way, for an Activitly (or Fragment) to observe Lifecycle event? Meaning, those call back event functions shouldn't be triggered, during configuration change, activity re-creation, ...



from How to make an Activitly correctly observe Lifecycle event

No comments:

Post a Comment