使用底部导航时保存片段状态

时间:2018-11-03 07:50:31

标签: android android-fragments android-bottom-nav-view

我正在使用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();


}

2 个答案:

答案 0 :(得分:0)

因为每当用户滑动到另一个选项卡时,您总会创建一个新的Fragment实例。解决方案是您可以使用HashMap存储在给定位置创建的片段。

private Map<Integer, Fragment> fragments = new HashMap<>();


bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){
    @Override
    public void onTabSelected(int position) {
        Fragment fragment = fragments.get(position);

        if (fragment == null){
            if (position == 0) {
                fragment = new Daily();
            } else if (position == 1) {
                fragment = new Trending();
            } else {
                fragment = new Random();
            } 

            fragments.put(position, fragment);
        }

        loadFragment(fragment);
    }
    @Override
    public void onTabUnselected(int position) {

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

答案 1 :(得分:0)

问题是您总是在任何Fragment上创建一个新的onTabSelected。因此,为了对其进行修复,您需要使用FragmentManager

可能的解决方案:使用addshow/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-可以优化代码。