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
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