showDialog deprecated. What's the alternative?
From http://developer.android.com/reference/android/app/Activity.html
public final void showDialog (int id) Added in API level 1
This method was deprecated in API level 13. Use the new DialogFragment
class with FragmentManager instead; this is also available on older
platforms through the Android compatibility package.Simple version of showDialog(int, Bundle) that does not take any
arguments. Simply calls showDialog(int, Bundle) with null arguments.
Why
- 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 API here, not with direct calls on the dialog.
- Here is a nice discussion
Android DialogFragment vs Dialog - Another nice discussion
DialogFragment advantages over AlertDialog
How to solve?
- http://android-developers.blogspot.in/2012/05/using-dialogfragments.html
More
- http://developer.android.com/guide/topics/fundamentals/fragments.html
- http://developer.android.com/training/basics/fragments/index.html
The method showDialog(int) from the type Activity is deprecated in android?
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
Simplest yes/no dialog fragment
A DialogFragment is really just a fragment that wraps a dialog. You can put any kind of dialog in there by creating and returning the dialog in the onCreateDialog() method of the DialogFragment.
Heres an example DialogFragment:
class MyDialogFragment extends DialogFragment{
Context mContext;
public MyDialogFragment() {
mContext = getActivity();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
alertDialogBuilder.setTitle("Really?");
alertDialogBuilder.setMessage("Are you sure?");
//null should be your on click listener
alertDialogBuilder.setPositiveButton("OK", null);
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return alertDialogBuilder.create();
}
}
To create the dialog call:
new MyDialogFragment().show(getFragmentManager(), "MyDialog");
And to dismiss the dialog from somewhere:
((MyDialogFragment)getFragmentManager().findFragmentByTag("MyDialog")).getDialog().dismiss();
All of that code will work perfectly with the support library, by just changing the imports to use the support library classes.
Android DialogFragment vs Dialog
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");
}
Callback to a Fragment from a DialogFragment
Activity involved is completely unaware of the DialogFragment.
Fragment class:
public class MyFragment extends Fragment {
int mStackLevel = 0;
public static final int DIALOG_FRAGMENT = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mStackLevel = savedInstanceState.getInt("level");
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("level", mStackLevel);
}
void showDialog(int type) {
mStackLevel++;
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
Fragment prev = getActivity().getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
switch (type) {
case DIALOG_FRAGMENT:
DialogFragment dialogFrag = MyDialogFragment.newInstance(123);
dialogFrag.setTargetFragment(this, DIALOG_FRAGMENT);
dialogFrag.show(getFragmentManager().beginTransaction(), "dialog");
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case DIALOG_FRAGMENT:
if (resultCode == Activity.RESULT_OK) {
// After Ok code.
} else if (resultCode == Activity.RESULT_CANCELED){
// After Cancel code.
}
break;
}
}
}
}
DialogFragment class:
public class MyDialogFragment extends DialogFragment {
public static MyDialogFragment newInstance(int num){
MyDialogFragment dialogFragment = new MyDialogFragment();
Bundle bundle = new Bundle();
bundle.putInt("num", num);
dialogFragment.setArguments(bundle);
return dialogFragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle(R.string.ERROR)
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(R.string.ok_button,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, getActivity().getIntent());
}
}
)
.setNegativeButton(R.string.cancel_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, getActivity().getIntent());
}
})
.create();
}
}
Calling DialogFragment
Here the solution:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.image_detail_fragment,
container, false);
mImageView = (RecyclingImageView) v.findViewById(R.id.imageView);
mImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
MyDialogFragment dialog = MyDialogFragment.newInstance();
dialog.show(getActivity().getFragmentManager(), "MyDialogFragment");
}
});
return v;
}
Activity.showDialog(int), Activity.removeDialog(int) deprecated
Android UI is now based on fragments, so you should use a DialogFragment.
It's included in the support library v4, so you can use it also in Android v2.1.
Blog post with example:
http://android-developers.blogspot.co.il/2012/05/using-dialogfragments.html
Documentation:
http://developer.android.com/reference/android/support/v4/app/DialogFragment.html
Related Topics
Android 'Unable to Add Window -- Token Null Is Not for an Application' Exception
How to Do Circular Scrolling on Viewpager
How to Test If a Fragment View Is Visible to the User
Identifying Rtl Language in Android
How to Change the Background Color Around a Dialogfragment
How to Draw Road Directions Between Two Geocodes in Android Google Map V2
How to Display Border to Imageview
Preventing/Catching "Illegalargumentexception: Parameter Must Be a Descendant of This View" Error
How to Use Device Default Theme for App
Android Studio Create Project Template for New Projects
Detect If an App Is Installed from Play Store
How to Change the Color of the Tabs Indicator Text in Android
Why Does Gradle Build My Module in Release Mode When the App Is in Debug
Android. Scrolling 2 Listviews Together
Update Text of Notification, Not Entire Notification