当我再次单击底部导航项时,片段再次加载

时间:2018-05-28 06:04:43

标签: java android android-studio

这是我的代码我创建了一个底部导航视图,我将这些项目分别连接到片段。当我再次单击导航项时,片段再次加载。

我不知道这里有什么问题。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnNavigationItemSelectedListener(navListener);

    }
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {

                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment SelectedFragment=null;

                    FragmentManager fm=getSupportFragmentManager();
                    FragmentTransaction ft=fm.beginTransaction();
                    ft.commit();


                    switch (item.getItemId()) {
                        case R.id.navigation_home:

                            setTitle("Playing11");
                            HomeFragment fragment = new HomeFragment();
                            ft.replace(R.id.fragment_container, fragment);
                            return true;

                        case R.id.navigation_dashboard:
                            HomeFragment1 fragment1 = new HomeFragment1();
                            ft.replace(R.id.fragment_container, fragment1);

                            return true;
                        case R.id.navigation_notifications:

                            setTitle("Safe11");
                            HomeFragment2 fragment2 = new HomeFragment2();
                            ft.replace(R.id.fragment_container, fragment2);

                            return true;
                        case R.id.navigation_notification:
                            setTitle("More");
                            HomeFragment3 fragment3 = new HomeFragment3();
                            ft.replace(R.id.fragment_container, fragment3);

                            return true;
                    }

                    return false;
                }
            };

}

2 个答案:

答案 0 :(得分:1)

尝试更改您的代码:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    switch (item.getItemId()) {
        case R.id.menu_home:

            HomeFragment homeFragment = new HomeFragment();
            fragmentTransaction.replace(R.id.frameContentPlace, homeFragment, "HomeFragment").addToBackStack("HomeFragment");
            break;
        case R.id.menu_profile:

            HomeFragment1 fragment1 = new HomeFragment1();
            fragmentTransaction.replace(R.id.frameContentPlace, fragment1).addToBackStack("HomeFragment1");
            break;
        case R.id.menu_bookings:

            HomeFragment2 fragment2 = new HomeFragment2();
            fragmentTransaction.replace(R.id.frameContentPlace, fragment2).addToBackStack("HomeFragment2");
            break;
    }
    fragmentTransaction.commit();

    return true;
}

答案 1 :(得分:-1)

添加片段使用标记时。所以写

ft.replace(R.id.fragment_container, fragment, "MyFragment);

而不是

ft.replace(R.id.fragment_container, fragment);

并检查此片段是否可见,以便不再次加载此片段

switch (item.getItemId()) 
{
    case R.id.navigation_home:
    HomeFragment fragment = (HomeFragment)getFragmentManager().findFragmentByTag("MyFragment");
    if (fragment != null && !fragment.isVisible()) 
    {
        setTitle("Playing11");
        HomeFragment fragment = new HomeFragment();
        ft.replace(R.id.fragment_container, fragment);
        return true;
    }
}