Opening Fragment from a DialogFragment (replacing the Dialogs parent)
Asked Answered
M

3

6

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?

Muscarine answered 1/2, 2013 at 16:5 Comment(0)
T
3

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

Token answered 22/6, 2013 at 14:16 Comment(1)
Is there a way to open a fragment without closing dialogfragment. because till we do not dismiss dialogfragment , fragment does not show because dialogfragment is overlapping the new fragmentPlautus
E
2

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. (:

Emunctory answered 13/3, 2019 at 11:23 Comment(0)
A
0

You shouldn't be opening the dialog from FragmentA. Implement a callback from your FragmentA to your activity and let the activity handle all fragment transactions.

Alba answered 1/2, 2013 at 16:28 Comment(5)
It still just blinks over and right after Fragment A appears. When clicking back / up, I get same action as when I clicked back / up from Fragment A, so Fragment B was neither displayed (for more than half a second) nor added to back stack.Muscarine
I've rebuild the concept without problems... try using FragmentActivity fragmentActivity = getActivity(); instead of what you're doing: FragmentActivity fragmentActivity = (FragmentActivity) view.getContext();Alba
But I must add, that I used a different approach then you. Whenever I call another Fragment. I put the code in the Fragment Activity and let it handle the transactions. I think it's cleaner coding, since Fragments shouldn't call or remove each other. It's the Activites job to do so.Alba
Yeah, your approach is definitely cleaner then mine.. Gonna change this in all over my app. This, however, didn't solve the problem. The strange thing is, that the Fragment B actually appears for a fraction of second and then is replaced by Fragment A again.Muscarine
What happens if you remove this line Fragment prev = fragmentActivity.getSupportFragmentManager().findFragmentByTag("dialog"); if (prev != null) { ft.remove(prev); }Alba

© 2022 - 2024 — McMap. All rights reserved.