Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to unregister Activity lifecycle callbacks?

Tags:

In the Android Application class you can register/unregister ActivityLifecycleCallbacks, which allow you to monitor Activity lifecycle events in your application all in one place (if you so desire).

Where is the proper place to call unregisterActivityLifecycleCallbacks? I'm registering in onCreate, and was thinking that onTerminate would be the proper place just from the name of the method but the docs say for onTerminate():

This method is for use in emulated process environments. It will never be called on a production Android device, where processes are removed by simply killing them; no user code (including this callback) is executed when doing so.

I don't see any other methods in the Application class where a 'shutdown' type callback is made. Do you even need to unregister?

like image 387
Christopher Perry Avatar asked Jul 25 '13 17:07

Christopher Perry


1 Answers

Simon was right about this. registerActivityLifecycleCallbacks doesn't require a matching call to unregisterActivityLifecycleCallbacks. As described in this SO question you can't detected when your application is being killed. So there is no "logical" place to unregister your callback. And even if there was, your Application instance is killed after the last Activity action takes place, and the killing of your Application kills the mActivityLifecycleCallbacks list which holds the reference to your callback anyway.

TL;DR - Don't worry about calling unregisterActivityLifecycleCallbacks, it's only there if you want to stop tracking Activity actions during your Application's normal lifecycle.

like image 125
bclymer Avatar answered Sep 20 '22 18:09

bclymer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!