Opening Fragment from a DialogFragment (replacing the Dialogs parent)

穿精又带淫゛_ 提交于 2019-12-05 09:14:58

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

Prash

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

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.

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