中方向变化的片段对话框消失对话框、片段、方向

2023-09-05 04:08:21 作者:漸莣那傷

所以,我不是一个很经验的Andr​​oid程序员,所以请善待我:)

So I am not a very experience Android programmer, so please be gentle with me :)

我想创建一个使用fragements一个应用程序,并从在这些片段中的一个,我调用自定义对话框。

I am trying to create an app that uses fragements and from within one of these fragments I am calling a custom dialog box.

//create dialog
final Dialog dialog = new Dialog(getActivity());
dialog.setCancelable(false);
dialog.setContentView(R.layout.fragment_update_dialog);

//set up data in dialog here

Button bUpdate = (Button) dialog.findViewById(R.id.bDialogUpdate);
bUpdate.setOnClickListener(new OnClickListener() 
{
//define onclick listener code here
});
dialog.show();

这code正常工作,我没有问题了。但是,当有屏幕方向的变化,然后我的对话框消失。

This code works fine and I have no issues with it. But when there is a screen orientation change then my dialog box disappears.

现在我已看过一些帖子在这个论坛上,和其他地方,所以我理解为什么这是存在的,但我没有找到一个解决办法来阻止它。

Now I have read several posts on this forum, and other places, so I understand why this is occuring, but I don't find a solution to stop it.

我已经试过了'把戏'与清单文件,但它不工作。 (也许是因为它是在片段和不活动?)

I have tried the 'trick' with the manifest file, but it doesn't work. (Perhaps because it is in the fragment and not the activity?)

我的清单文件包括;

<activity
        android:name="com.mycompany.myapp.MainActivity"
        android:configChanges="keyboardHidden|orientation"
....

和我的主要活动我有

@Override
public void onConfigurationChanged(Configuration newConfig) 
{
    super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_main);
}

但是,这并不正常工作。

But this doesn't work.

我也看到很多评论说,这不是recommened,但看不到如何解决这个问题。

I also see many comments say that this is not recommened, but can't see how to solve this issue.

感谢您的帮助

推荐答案

截至13 API有一个新值configChanges属性,屏幕尺寸

Up to API 13 there was a new value to the configChanges attribute, screenSize

因此​​,如果您使用的是大屏幕一定要增加屏幕尺寸在configChanges属性:

So if you're using large screens make sure to add screenSize in your configChanges attribute:

android:configChanges="orientation|keyboardHidden|screenSize"

<activity
    android:name=".MyMainActivity"
    android:configChanges="orientation|keyboardHidden|screenSize" 
    android:label="@string/app_name" >

参考