1

I am using bottom navigation drawer to switch between fragments, the problem is that every time I switch back to a fragment it gets recreated.

How can I save the state of fragment and resume it when switched back to it?

        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){
            @Override
            public void onTabSelected(int position) {
                if (position==0){
                    loadFragment(new Daily());
                }
                if (position==1){
                    loadFragment(new Trending());

                }
                if (position==2){
                    loadFragment(new Random());
                }
            }
            @Override
            public void onTabUnselected(int position) {

            }
            @Override
            public void onTabReselected(int position) {
            }
        });

 private void loadFragment(Fragment fragment) {
    // load fragment
    FragmentTransaction transaction = 
    getSupportFragmentManager().beginTransaction();
    transaction.attach( fragment);
    transaction.addToBackStack(null);
    transaction.commit();


}
André Sousa
  • 1,692
  • 1
  • 12
  • 23
Frisky Coder
  • 103
  • 14

1 Answers1

1

The issue is that you are always creating a new Fragment on any onTabSelected. So in order to fix it, you need to work with your FragmentManager.

Possible solution: use the add and show/hide methods.

Example:

private static final String DAILY_TAG = BuildConfig.APPLICATION_ID + ".DAILY_TAG";
private static final String TRENDING_TAG = BuildConfig.APPLICATION_ID + ".TRENDING_TAG";
private static final String RANDOM_TAG = BuildConfig.APPLICATION_ID + ".RANDOM_TAG";

public void onTabSelected(int position) {
    FragmentManager fragmentManager = getSupportFragmentManager();

    if (position == 0) {
        hideFragment(TRENDING_TAG)
        hideFragment(RANDOM_TAG)

        Fragment fragment = fragmentManager.findFragmentByTag(DAILY_TAG);
        FragmentTransaction transaction = fragmentManager.beginTransaction()

        if (fragment != null) {
            transaction.show(fragment)
        } else {
            transaction.add(content.id, new Daily(), DAILY_TAG)
        }

        transaction.commitNow()

    } else if (position == 1) {
        hideFragment(DAILY_TAG)
        hideFragment(RANDOM_TAG)

        Fragment fragment = fragmentManager.findFragmentByTag(TRENDING_TAG);
        FragmentTransaction transaction = fragmentManager.beginTransaction()

        if (fragment != null) {
            transaction.show(fragment)
        } else {
            transaction.add(content.id, new Trending(), TRENDING_TAG)
        }

        transaction.commitNow()

    } else {
        hideFragment(TRENDING_TAG)
        hideFragment(DAILY_TAG)

        Fragment fragment = fragmentManager.findFragmentByTag(RANDOM_TAG);
        FragmentTransaction transaction = fragmentManager.beginTransaction()

        if (fragment != null) {
            transaction.show(fragment)
        } else {
            transaction.add(content.id, new Random(), RANDOM_TAG)
        }

        transaction.commitNow()
    } 

    fragments.put(position, fragment);

    loadFragment(fragment);
}

private void hideFragment(String tag) {
    FragmentManager fragmentManager = getSupportFragmentManager()
    Fragment currentFragment = fragmentManager.findFragmentByTag(tag)

    if (currentFragment != null) {
        fragmentManager.beginTransaction().hide(currentFragment).commitNow()
    }
}

PS - The code can be optimized.

André Sousa
  • 1,692
  • 1
  • 12
  • 23
  • Wouldn't it consume more memory, 'cause we are hiding and showing multiple fragment UI? – Vicky Jul 11 '19 at 10:16