Fragment re-created on bottom navigation view item selected

后端 未结 15 2609
长发绾君心
长发绾君心 2020-12-07 23:55

Following is my code for bottom navigation view item selected

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationI         


        
15条回答
  •  忘掉有多难
    2020-12-08 00:43

    I faced the same problem and finally i i found the solution, you can try this code. it's work for me.

    import android.support.annotation.NonNull;
    import android.support.design.widget.BottomNavigationView;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.MenuItem;
    import android.widget.FrameLayout;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    public BottomNavigationView bv;
    public home_fragment home;
    public account_fragment afrag;
    public other_fragment other;
    public FrameLayout fr;
    android.support.v4.app.Fragment current;
    //public FragmentTransaction frt;
        public static int temp=0;
        final Fragment fragment11 = new account_fragment();
        final Fragment fragment22 = new home_fragment();
        final Fragment fragment33 = new other_fragment();
        final FragmentManager fm = getSupportFragmentManager();
        Fragment active = fragment11;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            bv=(BottomNavigationView) findViewById(R.id.navigationView);
    
    
            fm.beginTransaction().add(R.id.main_frame, fragment33, "3").hide(fragment33).commit();
            fm.beginTransaction().add(R.id.main_frame, fragment22, "2").hide(fragment22).commit();
            fm.beginTransaction().add(R.id.main_frame,fragment11, "1").commit();
    bv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
    
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.account:
                            fm.beginTransaction().hide(active).show(fragment11).commit();
                            active = fragment11;
                            return true;
    
                        case R.id.home1:
                            fm.beginTransaction().hide(active).show(fragment22).commit();
                            active = fragment22;
                            return true;
    
                        case R.id.other:
                            fm.beginTransaction().hide(active).show(fragment33).commit();
                            active = fragment33;
                            return true;
                    }
                    return false;
                }
            });
          bv.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
              @Override
              public void onNavigationItemReselected(@NonNull MenuItem item) {
                  Toast.makeText(MainActivity.this, "Reselected", Toast.LENGTH_SHORT).show();
    
              }
          });
    
    
        }
    
    }
    

提交回复
热议问题