Opening Fragment from a DialogFragment (replacing the Dialogs parent)

心已入冬 提交于 2020-01-13 10:21:51

问题


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 the fraction of second and then is replaced (covered?) by Fragment A again.

When I click up button I get even back from Fragment A so non of these transaction were added to the back stack. I would like to show Fragment B and then when pressing up button go back to Fragment A.

Is it somehow possible? And what's wrong on my approach?


回答1:


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




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




回答3:


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.



来源:https://stackoverflow.com/questions/14650298/opening-fragment-from-a-dialogfragment-replacing-the-dialogs-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!