如何显示在android系统回吐屏幕方向变化考虑进去对话框 [英] how to show dialogs in android taking screen orientation changes into account
问题描述
这是推动我疯了。我已阅读文档在Android中创建对话框,它似乎是有道理的,屏幕方向改变时除外。
我有Activity.onCreateDialog()
在这里我创建对话框。
我也有Activity.on prepareDialog()
在这里我prepare对话框。
然后在我的Activity.onCreate()方法,
我打电话的ShowDialog(ID)
这是一切都很好,并表现正常。现在到了棘手的部分。我旋转屏幕。它破坏活动,并创建一个新的。 Activity.onCreate()被调用,从而调用的ShowDialog(ID),但已经有一个对话在那里,所以它缠调用onCreateDialog()两次和prepareDialog()的两倍。什么是最好的做法在这里管理一个对话框时,你需要调用的ShowDialog在Activity.onCreate屏幕方向变化?
的http://groups.google.com/group/android-developers/browse_thread/thread/bf4c7798ee378d2
该网页上说:
更改为:
公共无效的onCreate(捆绑包)
{
....
//包是空的第一次启动在重新启动,非空
// [而不只是如果(条件为真)]
如果(条件为真放;&安培;捆绑== NULL)
的ShowDialog(42);
....}
块引用>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 ?
解决方案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); .... }
这篇关于如何显示在android系统回吐屏幕方向变化考虑进去对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!