The method showDialog(int) from the type Activity is deprecated in android?

 

up vote6down votefavorite

 




The method showDialog(int) from the type Activity is deprecated.

What's the reason? and how to solve it?



 

 

 

7down voteaccepted


What's the reason?

http://developer.android.com/reference/android/app/Activity.html#showDialog(int)

Android DialogFragment vs Dialog

How to solve it?

Use the new DialogFragment class with FragmentManager instead; this is also available on older platforms through the Android compatibility package.

http://android-developers.blogspot.in/2012/05/using-dialogfragments.html


share|improve this answer

 

 

 

 

 

 

Android DialogFragment vs Dialog

 

up vote84down votefavorite

34




Google recommends that we use DialogFragment instead of a simple Dialog by using Fragments API, but it is absurd to use an isolated DialogFragment for a simple Yes-No confirmation message box. What is the best practice in this case?


android android-fragments android-dialog android-dialogfragment


 

add a comment

share|improve this question


edited Jun 19 '13 at 21:50



samurai413x72



asked Nov 2 '11 at 8:18



skayred2,23432049



4 Answers

activeoldestvotes

up vote30down voteaccepted


Yes use DialogFragment and in onCreateDialog you can simply use an AlertDialog builder anyway to create a simple AlertDialog with Yes/No confirmation buttons. Not very much code at all.

With regards handling events in your fragment there would be various ways of doing it but I simply define a message Handler in my Fragment, pass it into the DialogFragment via its constructor and then pass messages back to my fragment's handler as approprirate on the various click events. Again various ways of doing that but the following works for me.

In the dialog hold a message and instantiate it in the constructor:

private Message okMessage;
...
okMessage = handler.obtainMessage(MY_MSG_WHAT, MY_MSG_OK);

Implement the `onClickListener' in your dialog and then call the handler as appropriate:

public void onClick(.....
    if (which == DialogInterface.BUTTON_POSITIVE) {
        final Message toSend = Message.obtain(okMessage);
        toSend.sendToTarget();
    }
 }

Edit

And as Message is parcelable you can save it out in onSaveInstanceState and restore it

outState.putParcelable("okMessage", okMessage);

Then in onCreate

if (savedInstanceState != null) {
    okMessage = savedInstanceState.getParcelable("okMessage");
}


 

show 9 more comments

share|improve this answer


edited Nov 2 '11 at 12:41

 

 



answered Nov 2 '11 at 11:50



PJL5,48234754


 

up vote15down vote


I would recommend using DialogFragment.

Sure, creating a "Yes/No" dialog with it is pretty complex considering that it should be rather simple task, but creating a similar dialog box with Dialog is surprisingly complicated as well.

(Activity lifecycle makes it complicated - you must let Activity manage the lifecycle of the dialog box - and there is no way to pass custom parameters e.g. the custom message to Activity.showDialog if using API levels under 8)

The nice thing is that you can usually build your own abstraction on top of DialogFragment pretty easily.


 

add a comment

share|improve this answer


edited Dec 19 '11 at 11:23

 

 



answered Nov 2 '11 at 8:40



hrnt5,8401728


up vote8down vote


Use DialogFragment over AlertDialog:


  • Since the introduction of API level 13:
    the showDialog method from Activity is deprecated. Invoking a dialog elsewhere in code is not advisable since you will have to manage the the dialog yourself (e.g. orientation change).
  • Difference DialogFragment - AlertDialog
    Are they so much different? From Android reference regarding DialogFragment:

A DialogFragment is a fragment that displays a dialog window, floating on top of its activity's window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment's state. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the APIhere, not with direct calls on the dialog.

  • Other notes
  • Fragments are a natural evolution in the Android framework due to the diversity of devices with different screen sizes.
  • DialogFragments and Fragments are made available in the support library which makes the class usable in all current used versions of Android.


 

add a comment

share|improve this answer


answered Jun 14 '13 at 23:27



user12817502,64421442


up vote7down vote


You can create generic DialogFragment subclasses like YesNoDialog and OkDialog, and pass in title and message if you use dialogs a lot in your app.

public class YesNoDialog extends DialogFragment
{
    public YesNoDialog()
    {

    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        Bundle args = getArguments();
        String title = args.getString("title", "");
        String message = args.getString("message", "");

        return new AlertDialog.Builder(getActivity())
            .setTitle(title)
            .setMessage(message)
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);
                }
            })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, null);
                }
            })
            .create();
    }
}

Then call it using the following:

DialogFragment dialog = new YesNoDialog();
    Bundle args = new Bundle();
    args.putString("title", title);
    args.putString("message", message);
    dialog.setArguments(args);
    dialog.setTargetFragment(this, YES_NO_CALL);
    dialog.show(getFragmentManager(), "tag");

And handle the result in onActivityResult.


 

share|improve this answer


edited Jan 9 at 22:57

 

 



answered Jan 9 at 22:51



ashishduh1,7241512


 



阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6