如何在 Android 中显示对话框并考虑屏幕方向的变化

发布于 2024-10-27 03:28:49 字数 437 浏览 4 评论 0原文

这让我发疯。我已经阅读了在 Android 中创建对话框的文档,它似乎很有意义,除非屏幕方向发生变化。

我有 Activity.onCreateDialog() 我在哪里创建对话框。

我也有 Activity.onPrepareDialog() 我在那里准备对话。

然后在我的 Activity.onCreate() 方法中, 我调用 showDialog(id) ,

一切都很好并且行为正常。现在是棘手的部分。我旋转屏幕。它破坏了活动并创建了新的活动。 Activity.onCreate() 被调用,它又调用 showDialog(id),但已经有一个对话框了,所以它最终会调用 onCreateDialog() 两次和 onPrepareDialog() 两次。当屏幕方向发生变化并且需要在 Activity.onCreate 中调用 showDialog 时,管理对话框的最佳方法是什么?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半﹌身腐败 2024-11-03 03:28:49

您可以在方法 onSaveInstaceState 内将对话框实例保存在捆绑包中,并在方法 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);
}

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);
}
横笛休吹塞上声 2024-11-03 03:28:49

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

如该页面所述:

更改为:

public void onCreate(Bundle 捆绑)
{
   ....
   // 第一次启动时bundle为null,重新启动时为非null
   // [而不仅仅是“if(条件为真)”]
   if (条件为真 &&bundle == null)
       显示对话框(42);
   ....

}

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);
   ....

}
孤单情人 2024-11-03 03:28:49

您可以将 Dialog 实现为 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();
    }

}

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();
    }

}

青芜 2024-11-03 03:28:49

如果您不关心轮换,您可以创建自己的代码来管理此操作:

清单更改:

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

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">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文