Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment re-created on bottom navigation view item selected

Following is my code for bottom navigation view item selected

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {   @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) {     Fragment fragment = null;     switch (item.getItemId()) {         case R.id.action_one:             // Switch to page one             fragment = FragmentA.newInstance();             break;         case R.id.action_two:             // Switch to page two             fragment = FragmentB.newInstance();             break;         case R.id.action_three:             // Switch to page three             fragment = FragmentC.newInstance();             break;     }     getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment,"TAG").commit();     return true; } }); 

Now my problem is every time fragment is re-created and don't want fragment to be recreated every time I also tried adding addToBackStack(null) but it this case on back button press keeps popping fragments from stack which I don't want.

Is there any way to display fragments on bottom navigation bar selected without re-creating fragment

like image 210
amodkanthe Avatar asked Feb 21 '17 09:02

amodkanthe


People also ask

How do I hide the bottom navigation bar in fragments?

To do this, use SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION . You may also need to use SYSTEM_UI_FLAG_LAYOUT_STABLE to help your app maintain a stable layout. When you use this approach, it becomes your responsibility to ensure that critical parts of your app's UI don't end up getting covered by system bars.


2 Answers

With support library v26 you can do this

FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();  Fragment curFrag = mFragmentManager.getPrimaryNavigationFragment(); if (curFrag != null) {     fragmentTransaction.detach(curFrag); }  Fragment fragment = mFragmentManager.findFragmentByTag(tag); if (fragment == null) {     fragment = new YourFragment();     fragmentTransaction.add(container.getId(), fragment, tag); } else {     fragmentTransaction.attach(fragment); }  fragmentTransaction.setPrimaryNavigationFragment(fragment); fragmentTransaction.setReorderingAllowed(true); fragmentTransaction.commitNowAllowingStateLoss(); 
like image 52
Viven Avatar answered Sep 23 '22 21:09

Viven


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();            }       });       }  } 
like image 36
Mazen Jameel Avatar answered Sep 22 '22 21:09

Mazen Jameel