简体   繁体   中英

Android bottom navigation fragments issue

I'm using bottom navigation with fragments in my activity. I have four fragments for each navigation item. Everything works fine, except one moment. In onCreate i use this code to load first fragment when activity created:

   //Manually displaying the first fragment - one time only

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.frame_layout, MyCoursesFragment.newInstance("",""));
            transaction.commit();

And my BottomNavigationListener is:

 private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment selectedFragment = null;
        switch (item.getItemId()) {
            case R.id.navigation_home:
                selectedFragment = MyCoursesFragment.newInstance("","");
                break;
            case R.id.navigation_dashboard:
                selectedFragment = CatalogFragment.newInstance("","");
                break;
            case R.id.navigation_notifications:
                selectedFragment = MessagesFragment.newInstance("","");
                break;
            case R.id.my_people:
                selectedFragment = MyPeopleFragment.newInstance("","");
                break;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_layout, selectedFragment);
        transaction.commit();
        return true;
    }

};

So, when i rotate the phone and screen orientation changes, i always get first Fragment (because of the first piece of code from onCreate).

Without it, i'll get the right fragment after rotation, but at the first open the screen will be empty.

How to get right fragment after rotation and also load first fragment when activity starts?

Activity is recreated after each rotation by default.

You can override this behavior with the configChanges attribute of the activity tag in AndroidManifest.

android:configChanges="orientation|screenSize"

Handling Configuration Changes

The above solution did not work for me.
What worked for me was to check if the savedInstanceState was null before loading the default fragment. This ensured that even on rotation, the default fragment is not shown by default if data belonging to another fragment exists in savedInstanceState

ie

if(savedInstanceState==null) {
   //This is the function you call onCreate to load default fragment
   loadFragments(new MyDefaultFragment());
}

只需检查 bundle(OnsavedInstanceSate) 是否为空,然后显示默认片段

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM