Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent dialog dismissal on screen rotation in Android

I am trying to prevent dialogs built with Alert builder from being dismissed when the Activity is restarted.

If I overload the onConfigurationChanged method I can successfully do this and reset the layout to correct orientation but I lose sticky text feature of edittext. So in solving the dialog problem I have created this edittext problem.

If I save the strings from the edittext and reassign them in the onCofiguration change they still seem to default to initial value not what was entered before rotation. Even if I force an invalidate does seem to update them.

I really need to solve either the dialog problem or the edittext problem.

Thanks for the help.

like image 232
draksia Avatar asked Sep 26 '11 15:09

draksia


People also ask

What can be done to prevent a dialog from disappearing when the device gets rotated?

The best way to avoid this problem nowadays is by using a DialogFragment . Create a new class which extends DialogFragment . Override onCreateDialog and return your old Dialog or an AlertDialog . Then you can show it with DialogFragment.

How do you handle rotation on Android?

If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.


1 Answers

The best way to avoid this problem nowadays is by using a DialogFragment.

Create a new class which extends DialogFragment. Override onCreateDialog and return your old Dialog or an AlertDialog.

Then you can show it with DialogFragment.show(fragmentManager, tag).

Here's an example with a Listener:

public class MyDialogFragment extends DialogFragment {      public interface YesNoListener {         void onYes();          void onNo();     }      @Override     public void onAttach(Activity activity) {         super.onAttach(activity);         if (!(activity instanceof YesNoListener)) {             throw new ClassCastException(activity.toString() + " must implement YesNoListener");         }     }      @Override     public Dialog onCreateDialog(Bundle savedInstanceState) {         return new AlertDialog.Builder(getActivity())                 .setTitle(R.string.dialog_my_title)                 .setMessage(R.string.dialog_my_message)                 .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {                      @Override                     public void onClick(DialogInterface dialog, int which) {                         ((YesNoListener) getActivity()).onYes();                     }                 })                 .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {                      @Override                     public void onClick(DialogInterface dialog, int which) {                         ((YesNoListener) getActivity()).onNo();                     }                 })                 .create();     } } 

And in the Activity you call:

new MyDialogFragment().show(getSupportFragmentManager(), "tag"); // or getFragmentManager() in API 11+ 

This answer helps explain these other three questions (and their answers):

  • Android Best way of avoid Dialogs to dismiss after a device rotation
  • Android DialogFragment vs Dialog
  • How can I show a DialogFragment using compatibility package?
like image 191
Brais Gabin Avatar answered Sep 21 '22 08:09

Brais Gabin