Calling Dialogfragment From Fragment (not Fragmentactivity)?
Solution 1:
When you create a new Dialog
, you can simply call it using this (very) simple method from a Fragment
.
DialogFragmentdialog= DialogFragment.instantiate(getActivity(), "Hello world");
dialog.show(getFragmentManager(), "dialog");
If you want to use your own dialog, please use that kind of code.
publicclassMyDialogFragmentextendsDialogFragment
{
//private View pic;publicMyDialogFragment()
{
}
@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState)
{
Viewview= getActivity().getLayoutInflater().inflate(R.layout.fragment_my_dialog, newLinearLayout(getActivity()), false);
// Retrieve layout elementsTextViewtitle= (TextView) view.findViewById(R.id.text_title);
// Set values
title.setText("Not perfect yet");
// Build dialogDialogbuilder=newDialog(getActivity());
builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
builder.getWindow().setBackgroundDrawable(newColorDrawable(android.graphics.Color.TRANSPARENT));
builder.setContentView(view);
return builder;
}
}
Solution 2:
it will help if you need to show a fragment dialog inside a fragment
DialogFragment
publicclassDialogBoxFragmentextendsDialogFragment {
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
ViewrootView= inflater.inflate(R.layout.dialog_fragment, container, false);
getDialog().setTitle("simple dialog");
return rootView;
}
}
Now showing the fragment dialog into a fragment
DialogFragmentdialogFragment=newDialogFragment ();
dialogFragment.show(getActivity().getFragmentManager(),"simple dialog");
Solution 3:
Check for import statements. If we use
ExampleDialogFragmentdialog=newExampleDialogFragment ();
dialog .show(getFragmentManager(), "example");
Then make sure to import
import android.app.DialogFragment;
import android.app.Fragment;
not from the support library.
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
Solution 4:
For me, it was the following: https://stackoverflow.com/a/25056160/2413303
The most important parts are that you need to have a Callback
for your dialog fragment:
publicclassMyFragmentextendsFragmentimplementsMyDialog.Callback
Which kinda works like this
publicclassMyDialogextendsDialogFragmentimplementsView.OnClickListener {
publicstaticinterfaceCallback
{
publicvoidaccept();
publicvoiddecline();
publicvoidcancel();
}
You make the Activity show the dialog for you from the Fragment:
MyDialogdialog=newMyDialog();
dialog.setTargetFragment(this, 1); //request code
activity_showDialog.showDialog(dialog);
Where showDialog()
for me was the following method:
@OverridepublicvoidshowDialog(DialogFragment dialogFragment)
{
FragmentManagerfragmentManager= getSupportFragmentManager();
dialogFragment.show(fragmentManager, "dialog");
}
And you call back onto your target fragment:
@OverridepublicvoidonClick(View v)
{
Callbackcallback=null;
try
{
callback = (Callback) getTargetFragment();
}
catch (ClassCastException e)
{
Log.e(this.getClass().getSimpleName(), "Callback of this class must be implemented by target fragment!", e);
throw e;
}
if (callback != null)
{
if (v == acceptButton)
{
callback.accept();
this.dismiss();
}
elseif (...) {...}
}
else
{
Log.e(this.getClass().getSimpleName(), "Callback was null.");
}
}
Solution 5:
Try this simple class I did in a myown project:
publicclassUIDialogMessageextendsDialogFragment {
publicstatic UIDialogMessage newInstance(int aTitleID, int aMessageID) {
return newInstance(aTitleID, aMessageID, true);
}
publicstatic UIDialogMessage newInstance(int aTitleID, int aMessageID, boolean aDoIt) {
UIDialogMessagefrag=newUIDialogMessage();
Bundleargs=newBundle();
args.putInt("titleID", aTitleID);
args.putInt("messageID", aMessageID);
args.putBoolean("keyBoolDoSomething", aDoIt);
frag.setArguments(args);
return frag;
}
@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
intmTitleID= getArguments().getInt("titleID");
intmMessageID= getArguments().getInt("messageID");
finalboolean mDoIt= getArguments().getBoolean("keyBoolDoSomething", true);
returnnewAlertDialog.Builder(getActivity())
.setTitle(mTitleID)
.setMessage(mMessageID)
.setPositiveButton(getResources().getString(R.string.dialog_button_gotcha),
newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
if (mDoIt)
doIt();
}
})
.create();
}
privatevoiddoIt() {
...
}
}
and you can call from a Fragment as shown below:
showDialog(R.string.dialog_title, R.string.dialog_message, false);
privatevoidshowDialog(int aTitleID, int aMessageID, boolean aDoIt) {
DialogFragment uiDialogMessage = UIDialogMessage.newInstance(aTitleID, aMessageID, aDoIt);
uiDialogMessage.show(getFragmentManager(), "dialog");
}
Post a Comment for "Calling Dialogfragment From Fragment (not Fragmentactivity)?"