如何在 Android 中显示对话框并考虑屏幕方向的变化
这让我发疯。我已经阅读了在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在方法
onSaveInstaceState
内将对话框实例保存在捆绑包中,并在方法onRestoreInstanceState
内再次显示对话框You can save your dialog instance in the bundle inside the method
onSaveInstaceState
and show the dialog again inside the methodonRestoreInstanceState
http://groups.google.com/group/android-developers/browse_thread /thread/bf4c7798ee378d2
如该页面所述:
http://groups.google.com/group/android-developers/browse_thread/thread/bf4c7798ee378d2
As stated on that page:
您可以将 Dialog 实现为 singleton 。
}
You can implement your Dialog as a singleton .
}
如果您不关心轮换,您可以创建自己的代码来管理此操作:
清单更改:
if you don't care about rotation you can create your own code for managing this:
Manifest Change: