Monday, April 9, 2018

How background activity handles configuration change on Android

It is well known that when configuration changes, activity will immediately call onDestroy and then onCreate to handle the new configuration.

But that only happens for the foreground activity. Any background activities will not handle the configuration change immediately when it happens. Instead, when the background activity was brought into foreground again, it will first calls onDestroy method and then calls onCreate method to handle the new configuration, in the similar way as the foreground activity. The intent that used to start the activity will also be available to initialize the activity when onCreate is called again for configuration change, however, as the activity is created from scratch, so the previous state information will be lost and need to to set again by application logic. 


Note onDestroy method cannot be used to check memory leak. For example, after calling finish() on the activity, if the activity is still referenced by a background thread, then although onDestroy is called on the activity, the activity object cannot be collected by GC, and will cause a memory leak. 

No comments:

Post a Comment