Dialog leak issue usually occur when the dialog is shown and later the activity destroy without dismissing it. Most common leaks happen while dialog is showing then the configuration or orientation change occur. So, it causes the leak and sometime it may also cause the App to crash or go into an invalid state. You may notice the leak only by looking at the logcat. This mean that you do not dismiss your dialog correctly before your activity get destroy.
When the orientation change, the activity will be destroyed and re-created again. Before the activity is destroyed due to orientation change, it will save the current state first. So, when the activity is re-created it tries to restore instance state (onRestoreInstanceState()) from the previous activity. Notice that when the activity is re-created, it is a new activity and a new view. The exception may occur, if it try to update the old view (possibly, the dialog that did not dismiss) in the new activity, because the old view will have an invalid Context.
So, if your application have any pop up dialog, make sure that you dismiss it before your application get destroy. Here is what you can do.
1. Dismiss all dialogs completely if exist in onPause() or immediately after it is no longer need.
2. Since most common leaks happen while dialog is showing then the configuration or orientation change occur, another way to avoid dialog leak is NOT to destroy the activity when orientation change. If the activity is not destroyed, then there is no dialog leak and there is no re-creation of new activity, thus no issue neither. This can be achieved by using the attribute android:configChanges=”orientation|keyboardHidden” in the manifest file and using the Override method onConfigurationChanged() inside your activity. By using the attribute android:configChanges you will tell Android that you will handle the configuration change yourself so, don’t destroy the activity. And you can handle the configuration change in the Override method, or you may leave it blank if you prefer not to do anything.
Wednesday, March 28, 2012
Avoid dialog leak issue in Android?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment