1

I am currently using an AlertDialog to create a simple dialog in my application. The code I am using looks like this:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle(DialogTitle);
builder.setItems(R.array.options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
    switch (which) {
        case 0:
            reset();
        break;
        case 1:
        exit();
        break;
        default:
        break;
       }
    }
});
builder.show();

I have read that the best option may be to create a class that extends DialogFragment and then use my DialogFragment instead of my current implementation.

Can anyone confirm that this is the best solution, suggest something better and possibly give me an example?

Thank you.

Tim Ashton
  • 436
  • 6
  • 18
  • 1
    Yes, using a `DialogFragment` would be the appropriate way to go these days. There's documentation and an example in the [Dialogs topic](http://developer.android.com/guide/topics/ui/dialogs.html#DialogFragment) on d.android.com. The benefits of having a `DialogFragment` is that the framework is able to manage its state correctly on configuration changes, and it becomes a little reusable component. Using `android:configChanges` (as suggested below) can have several undesired side effect and make for a less flexible code base. – MH. Nov 27 '13 at 09:01
  • I went with the DialogFragment. After poking around in the Dialogs topics I easily implemented one which met my needs. In my test activity it appears that I can now rotate the screen with the Fragment "out of the box". I have not yet implemented in my application. Thanks MH. – Tim Ashton Nov 27 '13 at 11:24

2 Answers2

1
public class ListDialogFragment extends DialogFragment 
    {
    // Use this instance of the interface to deliver action events
    private listDialogListener mListener;
    private String  title;
    private int items;

    /**
     * Create a new instance of EndGameDialogFragment, String dialogTitle
     * and ing dialogListItems as an argument.
     */
    static ListDialogFragment newInstance(String dialogTitle, int dialogListItems) 
    {
        ListDialogFragment frag = new ListDialogFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putString("title", dialogTitle);
        args.putInt("items", dialogListItems);
        frag.setArguments(args);

        return frag;
    }


    /* The activity that creates an instance of this dialog fragment must
     * implement this interface in order to receive event callbacks.
     * Each method passes the DialogFragment in case the host needs to query it. */
    public interface listDialogListener 
    {
        public void onDialogClick(DialogFragment dialog, int which);
    }

    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
    @Override
    public void onAttach(Activity activity) 
    {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try 
        {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (listDialogListener) activity;
        } 
        catch (ClassCastException e) 
        {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    {
        title = getArguments().getString("title"); //retrive the titleString
        items = getArguments().getInt("items"); //retrive array of items for the list (from strings.xml)

        //build the dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(title);
        builder.setItems(items, new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int which) 
            {
                switch(which)
                {
                    case 0:
                        mListener.onDialogClick(ListDialogFragment.this, which);
                        break;
                    case 1:
                        mListener.onDialogClick(ListDialogFragment.this, which);
                        break;
                    default:
                        break;
                }
            }
        });
        return builder.create();
    }

}
Tim Ashton
  • 436
  • 6
  • 18
  • I cannot see anything in this code which recreate the fragment correctly – Raneez Ahmed Dec 13 '13 at 04:41
  • Hello Reneez, I do not need to recreate the fragment. I was previously using a dialog directly and I was unaware that using a DialogFragment this way would give me all the functionality that I needed. When I implemented as above, the rotations of the screen are handled by the fragment lifecycle automatically and the dialog will not disapear when the screen is rotated, As was my problem before. – Tim Ashton Dec 20 '13 at 14:09
  • The fragment lifecycle - http://developer.android.com/guide/components/fragments.html Destroys and recreates the dialog just like an Activity when the screen is turned. – Tim Ashton Dec 20 '13 at 14:13
0

You can override the onConfigurationChanged(Configuration newConfig) method of your activity to recognize the orientation change and restore the AlertDialog if it was open. For this you will need the following tag in your activity definition in your manifest android:configChanges="orientation. A good introduction can be found here.

Baschi
  • 1,128
  • 11
  • 14