-1

i've this dialog

case DIALOGO_EDIT:

final EditText editText = new EditText(context);

builder.setView(editText);

builder.setPositiveButton("Send", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int i) {

    }
});

builder.setNegativeButton("Close", null);

break;

But when i rotate the device the dialog dismiss... How can i solve this problem and mantein the dialog during rotation? Or display the edit text at "full screen" like WhatsApp

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
user3103764
  • 87
  • 2
  • 10

2 Answers2

0

When the screen is rotated, the activity actually is restarted and killed, so you need to save and be able to restore the data using the lifecycle methods. Have a look here: Saving Persistent State

You may want to have a look to this question here

Community
  • 1
  • 1
COLD ICE
  • 840
  • 1
  • 12
  • 31
0

The best way of avoid this problem is to use DialogFragment.

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

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

Here an example with the activity like 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 kind of questions already asked,and there are also solution for it, these three questions are matched with your problem (and they answered):

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?

Community
  • 1
  • 1
Hamad
  • 5,096
  • 13
  • 37
  • 65