javaandroidandroid-studioandroid-fragmentsandroid-menu

How can I switch between Fragments without destroying them?


I'm playing with native Android apps navigation, using BottomNavigationBar to switch between Fragments:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // ...
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment = null;

                    switch (item.getItemId()) {
                        case R.id.first:
                            selectedFragment = new FirstFragment();
                            break;
                        case R.id.second:
                            selectedFragment = new SecondFragment();
                            break;
                        case R.id.third:
                            selectedFragment = new ThirdFragment();
                            break;
                    }

                    getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.container, selectedFragment)
                        .commit();

                    return true;

                }
            };
}

I want those Fragments to persist between navigations. Any suggestions?


Solution

  • Here's the best solution I could find:

    private SecondFragment second_fragment = new SecondFragment();
    private FirstFragment first_fragment = new FirstFragment();
    private ThirdFragment tird_fragment = new ThirdFragment();
        
    Fragment selected = second_fragment;`
    
    private void createFragment(Fragment fragment){
        getSupportFragmentManager()
            .beginTransaction()
            .add(R.id.container, fragment)
            .hide(fragment)
            .commit();
    }
    private void showFragment(Fragment fragment){
        getSupportFragmentManager()
            .beginTransaction()
            .show(fragment)
            .commit();
    }
    private void hideFragment(Fragment fragment){
        getSupportFragmentManager()
            .beginTransaction()
            .hide(fragment)
            .commit();
    }
    
    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                hideFragment(selected);
    
                switch (item.getItemId()) {
                    case R.id.first:
                        selected = first_fragment;
                        break;
                    case R.id.second:
                        selected = second_fragment;
                        break;
                    case R.id.third:
                        selected = third_fragment;
                        break;
                }
                
                showFragment(selected);
    
                return true;
        }
    };
    

    This way the menu just hides and shows Fragments visually.