How can I show this :
public class TagsDialog extends DialogFragment
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.tags_dialog, null));
builder.setMessage("This is a message").setTitle("TAGS");
return builder.create();
}
}
From inside my Fragment
inside a ViewPager
:
public class MyFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
...
ImageView btnTags = (ImageView)view.findViewById(R.id.btnTags);
btnTags.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
DialogFragment dlg = new TagsDialog();
//this line doesn't compile
dlg.show(getSupportFragmentManager(), "tags");
}
});
}
}
I have tried for ages to get this to work, but getSupportFragmentManager
is never resolved... any ideas?
EDIT:
I feel this is all caused by the support FragmentManager vs the android.app.FragmentManager, however I do not know how to solve this, as I am using the ViewPager from the support library...
getSupportFragmentManager/and all related getFragManager methods like parent and child one always returns the Manager from the support lib, wheras the show method wants the core one.
Imports are:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
FragmentManager fm = getActivity().getSupportFragmentManager(); // returns from support lib
DialogFragment dlg = new TagsDialog();
dlg.show(fm, "tags"); // wants core...
If I just use core, then getSupportFragmentManager()
does not exist on getActivity()
...
This method is deprecated. androidx. activity.
Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.
Showing the DialogFragment Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.
tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. 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. Dismiss the fragment and its dialog.
You should double check your imports. You can't use a mix of Fragment & FragmentManager from the support library and from android core apis.
If you use support, use everything from support (from package android.support.v4.app
). If not, use everything from core api package (android.app
).
Here is what I do in my Fragment that is in a ViewPager. The code to show a DialogFragment is the same where ever you would do it though.
FragmentManager fm = getActivity().getSupportFragmentManager();
ConfirmDeleteDialog dialog = new ConfirmDeleteDialog();
dialog.setTargetFragment(RecipeFragment.this, REQUEST_CONFIRM_RC);
Bundle b = new Bundle();
b.putString(ConfirmDeleteDialog.EXTRA_CONFIRM_DIALOG_TYPE,
ConfirmDeleteDialog.CONFIRM_DIALOG_TYPE.COMMIT.name()); // enum determines what to display
dialog.setArguments(b);
dialog.show(fm, "confirm delete");
EDIT.
I think you may be missing the *getActivity().*getSupportFragmentManager()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With