Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The best way to avoid this problem nowadays is by using a <a href="http://developer.android.com/reference/android/app/DialogFragment.html" rel="noreferrer"><code>DialogFragment</code></a>.</p> <p>Create a new class which extends <a href="http://developer.android.com/reference/android/app/DialogFragment.html" rel="noreferrer"><code>DialogFragment</code></a>. Override <a href="http://developer.android.com/reference/android/app/DialogFragment.html#onCreateDialog%28android.os.Bundle%29" rel="noreferrer"><code>onCreateDialog</code></a> and return your old <a href="http://developer.android.com/reference/android/app/Dialog.html" rel="noreferrer"><code>Dialog</code></a> or an <a href="http://developer.android.com/reference/android/app/AlertDialog.html" rel="noreferrer"><code>AlertDialog</code></a>.</p> <p>Then you can show it with <a href="http://developer.android.com/reference/android/app/DialogFragment.html#show%28android.app.FragmentManager,%20java.lang.String%29" rel="noreferrer"><code>DialogFragment.show(fragmentManager, tag)</code></a>.</p> <p>Here's an example with a <a href="http://developer.android.com/guide/components/fragments.html#EventCallbacks" rel="noreferrer"><code>Listener</code></a>:</p> <pre><code>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(); } } </code></pre> <p>And in the Activity you call:</p> <pre><code>new MyDialogFragment().show(getSupportFragmentManager(), "tag"); // or getFragmentManager() in API 11+ </code></pre> <p>This answer helps explain these other three questions (and their answers):</p> <ul> <li><a href="https://stackoverflow.com/q/11965154/842697">Android Best way of avoid Dialogs to dismiss after a device rotation</a></li> <li><a href="https://stackoverflow.com/q/7977392/842697">Android DialogFragment vs Dialog</a></li> <li><a href="https://stackoverflow.com/q/6746978/842697">How can I show a DialogFragment using compatibility package?</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload