• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Dialog is leaked on orientation change

kaliki

Newbie
Jan 21, 2010
11
0
Hi, built a simple app that displays an alertDialog with two buttons. The problem is it's not getting dismissed when i change orientation. After change i get a leak warning on debugger and the dialog appears twice(pressing back hides the first dialog, only a second back dismisses it completely). Any ideas?

Here is my code:

public class MyActivity extends Activity {

private static final int TEST_DIALOG = 0;

private AlertDialog alert;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.showDialog(TEST_DIALOG);
}

@Override
protected Dialog onCreateDialog(int id) {
switch(id){
case TEST_DIALOG:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
alert = builder.create();
alert.setMessage("Test Message");
alert.setButton("Start", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface arg0, int arg1){
alert.dismiss();
}
});
alert.setButton2("Exit", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface arg0, int arg1){
MyActivity.this.finish();
}
});
return alert;
}
return alert;
}

@Override
protected void onDestroy() {
super.onDestroy();
if(alert!=null)
alert.dismiss();
}
}

Warning i get:

Activity com.example.MyActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43b29210 that was originally added here
android.view.WindowLeaked: Activity com.example.MyActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43b29210 that was originally added here
at android.view.ViewRoot.<init>(ViewRoot.java:230)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:459)
at android.app.Dialog.show(Dialog.java:239)
at android.app.Activity.showDialog(Activity.java:2488)
at com.example.MyActivity.onCreate(MyActivity.java:22)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3625)
at android.app.ActivityThread.access$2300(ActivityThread.java:119)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1867)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Unexpected resume of com.example while already resumed in com.example
 
What it means when you "leak" a dialog is that somewhere you are holding a reference to it that cant be broken or nulled (or something in the activity is calling to the dialog asyncronously). This can happen with Threads, ASyncTasks, Runnables and inner classes.

So when onDestroy() tries to remove the activity it cannot remove the dialog.

In your case it appears you are calling to the dialog from your onDestroy(); method, but you should really be dismissing it earlier in onPause(); or onStop();

Once you have called super.onDestroy(); your activity should be ready to be destroyed.



When the phone's orientation changes, the default behavoir of the OS is to destroy the activity and recreate it in the new orientation.


jettimadhu's post should definitely help you. What it does is tell Android that you will handle the orientation change yourself, and that the Android OS should not tear down, and then recreaate the activity.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones