不要在BottomNavigationView中重新创建片段

时间:2017-12-28 14:38:20

标签: android android-fragments bottomnavigationview

问题:

每次单击BottomNavigationView时,都会重新创建片段。

目前在每个片段中,我都有Recycler View,在创建片段时只运行一次。

在这种情况下,每次都会加载RecyclerView数据。这不是我想要的。

我怎样才能使片段在第一次点击后保持活动状态。当你回到已经创建的片段时,只需显示它而不是每次都重新创建它?

我的尝试:

BottomNavigationView bottomNavigationView =(BottomNavigationView) findViewById(R.id.bottomNavView_Bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);

final Menu menu = bottomNavigationView.getMenu();
fragmentManager = getSupportFragmentManager();
final FragmentTransaction transaction = fragmentManager.beginTransaction();

/* Fragmento Start */
transaction.replace(R.id.content_home, new MainFragment()).commit();


bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Intent in;
        Fragment fragment = null;
        Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.content_home);
        switch (item.getItemId()){

            case R.id.ic_home:

                if (!(currentFragment instanceof MainFragment)) {
                    fragment = new MainFragment();
                    toolbar.setTitle("Main");
                    item = menu.getItem(0);
                    item.setChecked(true);
                }

                break;

            case R.id.ic_categorias:

                if (!(currentFragment instanceof CategoriasFragment)) {
                    fragment = new CategoriasFragment();
                    toolbar.setTitle("Categorias");
                    item = menu.getItem(1);
                    item.setChecked(true);
                }
                break;



            case R.id.ic_calendar:

                if (!(currentFragment instanceof FeedUserFragment)) {
                    fragment = new FeedUserFragment();
                    item = menu.getItem(2);
                    toolbar.setTitle("Feed");
                    item.setChecked(true);
                }
                break;

            case R.id.ic_explore:

                if (!(currentFragment instanceof ExploreFragment)) {
                    fragment = new ExploreFragment();
                    item = menu.getItem(3);
                    toolbar.setTitle("Explorar");
                    item.setChecked(true);
                }
                break;

            case R.id.ic_person:

                if (!(currentFragment instanceof Opcoes)) {
                    fragment = new Opcoes();
                    toolbar.setTitle("Opções");
                    item = menu.getItem(4);
                    item.setChecked(true);
                }
                break;

        }

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.content_home, fragment);
        transaction.commit();

        return false;

    }
});

2 个答案:

答案 0 :(得分:0)

  

您每次都在更换片段,即切换块后   完整。

解决方案:仅在条件满足时替换并使用有助于维护堆栈的awk -v RS= -F '[][]' '/enabled = 1/{print $2}' file 。您可以从this网站

了解更多信息

试试这个,

addToBackStack

答案 1 :(得分:0)

我会尝试transaction.add()并使用此方法findFragmentByTag搜索后堆栈中的片段。 https://developer.android.com/reference/android/support/v4/app/FragmentManager.html#findFragmentByTag(java.lang.String)