android tabs fragment navigation

217 views Asked by At

I want ask you about some not standard navigation pattern in android. So, my problem described below. If you do not have answer or some details do not understand, do not minus please. Just write comment and I correct my stuff. I do not use ViewPager or TabHost. Please see image to understand my expected navigation model:

Model

So, we have activity with 3 tabs. All tabs must be contain stack of fragments. First tab has only one fragment, so there no backstack logic. But another two tabs must have fragments logic with back way. Also user can switch all tab in any time.

To implement tabs I'm used BottomBar library.

I tried implement my solution with Stack[] array. And change fragments manually. But I receive memory leak. After many manipulation I understand here no way to find solution. So, I want ask, how I can implement solution with Android native backStack. Or any another solution.. Waiting for your answer. Thanks.

1

There are 1 answers

3
Avinash4551 On BEST ANSWER

You can add Fragments to back stack using below code.

fragmentTransaction.add(id, fragment, Tag(I usually do fragment.getclass().getName())).addToBackStack(backstackName(I usually do fragment.getclass().getName()))

When you add second fragment use the below repleaceFragentMethod.

  private void replaceFragment(android.support.v4.app.Fragment fragment) {
    String backStateName = fragment.getClass().getName();
    String fragmentTag = backStateName;

    FragmentManager manager = getSupportFragmentManager();
    manager.executePendingTransactions();
    boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);


    if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null) { //fragment not in back stack, create it.
        FragmentTransaction ft = manager.beginTransaction();
        ft.replace(R.id.frament_container, fragment, fragmentTag);
        ft.addToBackStack(backStateName);
        ft.commit();
    }
}

To find Memory Leaks Use LeakCanary(https://github.com/square/leakcanary)