I added this code

    @Override
    protected void onPause() {
        super.onPause();
        try{
            System.out.println("dismissing!!!");
            dismissDialog(TEST_DIALOG);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("in onresume!!");
        if(alert==null){
            System.out.println("Recreating dialog!");
            showDialog(TEST_DIALOG);
        }
    }

Unfortunately oncreatedialog is still fired twice, so onresume never
recreates it cause it's already there, the leak error as well. Any
ideas?

On Jun 24, 3:21 am, Brad Gies <[email protected]> wrote:
> You should dismiss it in OnPause and recreate it in OnResume if you
> still want it.
>
> On 23/06/2010 6:02 AM, Surfer wrote:
>
>
>
> > 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$Decor v...@43b29210 that
> > was originally added here
> > android.view.WindowLeaked: Activity com.example.MyActivity has leaked
> > window com.android.internal.policy.impl.PhoneWindow$Decor
> > v...@43b29210 that was originally added here
> > at android.view.ViewRoot.<init>(ViewRoot.java:230)
> > at android.view.WindowManagerImpl.addView(WindowManag erImpl.java:148)
> > at android.view.WindowManagerImpl.addView(WindowManag erImpl.java:91)
> > at android.view.Window$LocalWindowManager.addView(Win dow.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(I
> > nstrumentation.java:1047)
> > at android.app.ActivityThread.performLaunchActivity(A
> > ctivityThread.java:2459)
> > at android.app.ActivityThread.handleLaunchActivity(Ac
> > tivityThread.java:2512)
> > at
> > android.app.ActivityThread.handleRelaunchActivity( ActivityThread.java:
> > 3625)
> > at android.app.ActivityThread.access$2300(ActivityThr ead.java:119)
> > at android.app.ActivityThread$H.handleMessage(Activit yThread.java:
> > 1867)
> > at android.os.Handler.dispatchMessage(Handler.java:99 )
> > at android.os.Looper.loop(Looper.java:123)
> > at android.app.ActivityThread.main(ActivityThread.jav a:4363)
> > at java.lang.reflect.Method.invokeNative(Native Method)
> > at java.lang.reflect.Method.invoke(Method.java:521)
> > at com.android.internal.os.ZygoteInit$MethodAndArgsCa
> > ller.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
>
> > When changing orientation the activity gets destroyed and recreated
> > but after onCreate has finished creating the dialog, onCreateDialog is
> > run again, why is this happening?
>
> --
> Sincerely,
>
> Brad Gies
> -----------------------------------------------------------------------
> Bistro Bot - Bistro 
> Blurbhttp://www.bgies.comhttp://www.bistroblurb.comhttp://www.ihottonight.com
> -----------------------------------------------------------------------
>
> Never doubt that a small group of thoughtful, committed people can
> change the world. Indeed. It is the only thing that ever has - Margaret Mead

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to