开发者

Dialog being dismissed when activity restarts

开发者 https://www.devze.com 2023-02-17 02:03 出处:网络
When I开发者_Python百科 rotate the device when a dialog is shown, my activity is recreated and the dialog gets dismissed. Do I need to show the dialog again each time the user rotates the device?Your

When I开发者_Python百科 rotate the device when a dialog is shown, my activity is recreated and the dialog gets dismissed. Do I need to show the dialog again each time the user rotates the device?


Your activity is restarted when you rotate the device. You can stop this by adding this to your application part in the manifest:

android:configChanges="orientation|keyboardHidden"


Flavio,

You can prevent your Activity from being recreated on orientation changes by adding the following in your AndroidManifest.xml:

<activity android:configChanges="keyboardHidden|orientation" android:name="YourActivity"/>

Then you need to override the following method in your code:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}


Flavio: I am unable to recreate the problematic behavior with alertDialogs or custom dialogs on orientation change. Are you using onCreateDialog and showDialog(int id)? According to the docs "the dialog will be automatically saved and restored if you use showDialog. This is my experience.

It is even possible to create an XML layout with an editText widget, inflate the layout, and the state in the editText will be automagically saved on orientation change.

AlertDialog.Builder builder= new AlertDialog.Builder(this);
LayoutInflater inflater= getLayoutInflater();
final View myView= inflater.inflate(R.layout.alert_dialog_text_entry, null);
builder.setTitle("About");
builder.setMessage(alertMessage+"Version: "+versionName);
builder.setView(myView);
AlertDialog alert= builder.create();

I have code here and here with screen captures that demonstrates the persistence of the alertDialog and custom dialog on orientation change

0

精彩评论

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

关注公众号