I have five different fragments in my project, let's call them A...E. A and B are only visible for the user when he opens the app. Those are responsible for showing the loading screen (checking current login session) and the login screen itself. Fragment C ist the main screen of that application which the user can use to reach the other areas of the app. Because of that this, the fragments D and E got the "addToBackStack(String)" extra when the transaction have been created/commited.
My problem which is resulting into a blank screen, is when the user is opening D or E and goes back to C again. This action can lead to the blank screen for C the first time or redoing that step several times.
The relevant fragments C,D,E are using a view holder, databinding and recycler views. Each recycler view has multiple view types.
It seems like that onBackPressed to C is executing correctly but that C has been deleted from the cache or something similar.
Hope anyone can help me with that issue.
I found a possible solution for my problem. Still need to test this further but so far it seems to work:
private void showFragment(Fragment fragment, String tag, String backStackTag) {
FragmentTransaction ft = fragmentManager.beginTransaction();
if (backStackTag != null) {
ft.addToBackStack(backStackTag);
}
switch (tag) {
case "A":
case "B":
ft.replace(containerId, fragment, tag);
break;
case "C": {
Fragment main = fragmentManager.findFragmentByTag("C");
if (main != null) {
ft.show(main);
} else {
ft.replace(containerId, fragment, tag);
}
break;
}
case "D":
case "E": {
Fragment main = fragmentManager.findFragmentByTag("C");
ft.hide(main);
ft.add(containerId, fragment, tag);
break;
}
default:
ft.add(containerId, fragment, tag);
break;
}
ft.commit();
}