Show dialog from fragment?
You should use a DialogFragment instead.
Calling DialogFragment from Fragment (not FragmentActivity)?
When you create a new Dialog
, you can simply call it using this (very) simple method from a Fragment
.
DialogFragment dialog = DialogFragment.instantiate(getActivity(), "Hello world");
dialog.show(getFragmentManager(), "dialog");
If you want to use your own dialog, please use that kind of code.
public class MyDialogFragment extends DialogFragment
{
//private View pic;
public MyDialogFragment()
{
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_my_dialog, new LinearLayout(getActivity()), false);
// Retrieve layout elements
TextView title = (TextView) view.findViewById(R.id.text_title);
// Set values
title.setText("Not perfect yet");
// Build dialog
Dialog builder = new Dialog(getActivity());
builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
builder.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
builder.setContentView(view);
return builder;
}
}
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();
}
}
How to display AlertDialog in a Fragment?
Replace context
with getActivity()
.
The ApplicationContext
should not be used for tasks such as creating Dialogs. As you are in a fragment you can instead get the Activity-Context simply by calling the Fragments getActivity()
method.
Related Topics
Play-Services-Ads Conflicts with Appcompat
Android Input Connection Error
Error Inflating Class Android.Support.V4.View.Viewpager
How to Paging Query from Firebase Using Android Firebaseui
Android in Navigation Drawer When I Click Anywhere Drawer Closes
Android - Expandable Textview with Animation
I Want Show Notification at 8:00 Am Everyday
How to Reduce Latency in Mediacodec Video/Avc Decoding
Picture Taken with Camera Intent Is Low Quality on Imageview (7.0+)
Fcm Remote Notifications Payload for iOS and Android
Eclipse Indexer Errors When Using Stl with Android Ndk
How to Bring a Background Task to The Front Below Honeycomb
Firebase Endat() Not Working with Date String
Error Inflating Class Com.Google.Android.Maps.Mapview
Placeholder Text for an Input Type="Number" Does Not Show in Webkit Ics
How to Get Raw Preview Data from Camera Object at Least 15 Frames Per Second in Android
Emulator: Warning: Quick Boot/Snapshots Not Supported on This Machine