Archive for September, 2011

Screen Rotation in a Dialog Themed Activity

Thursday, September 1st, 2011

One of the apps I am writing uses an Activity that is themed with the @android:style/Theme.Dialog theme. This means that when the user enters the new dialog-themed activity the previous activity can still be seen in the background of the current activity. Not only can the user see the activity in the background but it still receives Activity lifecycle events before the Dialog-themed activity in front of it.

This has implications in screen rotation and other configuration changes: I had some code that starts my dialog-themed activity when a certain condition was met in the onResume() callback for the background activity. Every time I did a screen rotation the condition was met and created a new activity with a startActivity() call. This caused an activity/memory leak and some very confusing behavior.

I solved it by adding the FLAG_ACTIVITY_REORDER_TO_FRONT to the intent I used to start the activity. Now it will re-use the dialog-themed activity on screen rotation rather than trying to create a new one every time.