1

bottom navigation drawer用来在片段之间切换,问题是每次我切换回片段时都会重新创建它。

如何保存片段的状态并在切换回片段时恢复它?

        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();


}
4

1 回答 1

1

问题是您总是Fragment在任何onTabSelected. 因此,为了修复它,您需要使用您的FragmentManager.

可能的解决方案:使用addandshow/hide方法。

例子:

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 - 代码可以优化。

于 2018-11-03T08:45:49.200 回答