Let's say I have Fragment A, from which I open a DialogFragment
like this:
FragmentActivity fragmentActivity = (FragmentActivity) view.getContext();
FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();
Fragment prev = fragmentActivity.getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
DialogFragment fragmentDialog = MyDialogFragment.newInstance();
fragmentDialog.show(ft, "dialog");
From this Dialog, after clicking (positive / neutral / negative) button, I want to open Fragment B, which should replace Fragment A.
In the Dialog's onClick method I run a callback method of parent Activity:
@Override
public void onClick(DialogInterface dialog, int which) {
switch(which) {
case DialogInterface.BUTTON_NEUTRAL:
detailsCallbacks.openMoreDetails();
break;
}
}
And finally my Activity's openMoreDetails()
method looks like this:
@Override
public void openMoreDetails() {
Fragment fragmentB = Fragment.newInstance();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, fragmentB);
ft.addToBackStack(null);
ft.commit();
}
What I get is strange. Fragment B blinks on the screen just for a fraction of a second and then is replaced (covered?) by Fragment A again.
When I click the 'up' button I get back from Fragment A, so none of these transactions were added to the back stack. I would like to show Fragment B and then, when pressing the 'up' button, go back to Fragment A.
Is it somehow possible? And what's wrong with my approach?
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.
Stay organized with collections Save and categorize content based on your preferences. This class was deprecated in API level 28.
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.
Android DialogFragments. DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.
Just had the same problem:
Fragment A display a custom dialog fragment.
At click on one of the buttons of the dialog fragment, I wanted to remove the dialog and show Fragment B.
Fragment B was displayed and instantly disappear. My screen was displaying Fragment A again.
What was wrong on my initial implementation:
private void onClickInscription() {
FragmentInscription frag = FragmentInscription.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.main, frag);
ft.addToBackStack(null);
ft.commit();
dismiss();
}
And the correct one:
private void onClickInscription() {
dismiss();
FragmentInscription frag = FragmentInscription.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.main, frag);
ft.addToBackStack(null);
ft.commit();
}
So try to call dismiss first on your dialog then apply the FragmentTransction
I know this it's been a long time since the problem was posted, but I solved it by adding a if(fragmentB.getView != null)
before the replace statement.
It finally doesn't do the flash and disappear thing. (:
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