开发者

how to show dialogs in android taking screen orientation changes into account

this is driving me crazy. i have read the documentation for creating dialogs in Android and it seems to make sense, except when the screen orientation changes.

I have Activity.onCreateDialog() where I create the dialog.

I also have Activity.onPrepareDialog() where I prepare the dialog.

Then in my Activity.onCreate() method, I call showDialog(id)

that's all well and good and behaves properly. now comes the tricky part. I rotate the screen. it destroys the activity and creates 开发者_JAVA百科a new one. Activity.onCreate() gets called, which in turn calls showDialog(id), but there's already a dialog out there, so it winds up calling onCreateDialog() twice and onPrepareDialog() twice. What is the best approach here for managing a dialog when the screen orientation changes and you need to call showDialog in Activity.onCreate ?


You can save your dialog instance in the bundle inside the method onSaveInstaceState and show the dialog again inside the method onRestoreInstanceState

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putInt("dialog", DIALOG_ID);
    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    if(savedInstanceState.containsKey("dialog")){
        showDialog(DIALOG_ID);
    }
    super.onRestoreInstanceState(savedInstanceState);
}


http://groups.google.com/group/android-developers/browse_thread/thread/bf4c7798ee378d2

As stated on that page:

Change to:

public void onCreate(Bundle bundle)
{
   ....
   //  bundle is null on first start, non-null on restart
   // [rather than just "if (condition is true)"]
   if (condition is true && bundle == null)
       showDialog(42);
   ....

}


You can implement your Dialog as a singleton .

static class MyProgressDialog extends ProgressDialog 
    {

    private static MyProgressDialog dialog = null;

    static MyProgressDialog newInstance( Context context )
    {
        if ( dialog == null )
        {
            dialog = new MyProgressDialog( context );


        }
        return dialog;
    }
    static MyProgressDialog getInstance()
    {

            return dialog ; 

    }

    public static  void destroyInstance()
    {
        if(dialog!= null && dialog.isShowing() ){
        dialog.dismiss(); 

        }
        dialog= null;
    }

    private MyProgressDialog( Context context )
        {
        super( context );
        }


    }

    @Override
   protected void onDestroy()
    {
    super.onDestroy();

    if ( progressDialog != null && progressDialog.isShowing() )
    {

        progressDialog.dismiss();

    }
    if( MyProgressDialog.getInstance()!= null){
      MyProgressDialog.destroyInstance();
    }

}


if you don't care about rotation you can create your own code for managing this:

Manifest Change:

    <activity android:name=".MainActivity" android:label="@string/app_name" 
android:configChanges="orientation">
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜