简体   繁体   中英

Android bottom navigation bar when press back go to previous fragment

Here is my navigation bar :

导航栏截图

There are 4 items in my bottom nav bar , I want it to back to previous fragment when I press the back button,

Here`s my xml :

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottomnav"
    android:layout_width="382dp"
    android:layout_height="52dp"
    android:layout_marginBottom="0dp"
    app:itemBackground="@color/backbeyez"
    app:itemIconTint="@drawable/nav_items_color"
    app:itemTextColor="@drawable/nav_items_color"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation"
    tools:layout_editor_absoluteY="515dp">

</android.support.design.widget.BottomNavigationView>

Here`s the code for MainActivity :

public class MainActivity extends AppCompatActivity {

public void switchorders() {
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.container, new OrdersLayout()).commit();
}

public void switchcredits() {
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.container, new CreditLayout()).commit();
}

public void switchworks() {
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.container, new ListLayout()).commit();
}

public void switchprofile() {
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.container, new ProfileLayout()).commit();
}

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


    BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomnav);
    BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);


    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()) {
                case R.id.navigation_orders:
                    switchorders();
                    break;

                case R.id.navigation_credit:
                    switchcredits();
                    break;

                case R.id.navigation_works:
                    switchworks();
                    break;

                case R.id.navigation_profile:
                    switchprofile();
                    break;
            }
            return true;
        }
    });

}

I also tried defining a back button with the statements below but they didn`t work :

public void aboutback(View view) {
    if (getSupportFragmentManager().getBackStackEntryCount() > 0)
    {
        boolean done = getSupportFragmentManager().popBackStackImmediate();
    }
}

and also "popbackstack()" and "popbackstackImmediate()" did not work!

If you want to handle backpress in fragment than you should add addToBackStack() when adding or replacing a fragment

try to add like that in your all fragment

public void switchorders() {
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.container, new OrdersLayout()).addToBackStack("TAG").commit();
}

It will be handle by default.

You can also handle in onBackPress() in activity if you want

Following code will set the icon of the home fragment icon of the bottom navigation to active, use accordingly to make active each icon on back press:

final BottomNavigationView mBottomNav = findViewById(R.id.nav_view);
MenuItem homeItem = mBottomNav.getMenu().getItem(0);
mBottomNav.setSelectedItemId(homeItem.getItemId());

Use AlertDialog On BackPressed try this,

@Override
public void onBackPressed() {

    new AlertDialog.Builder(this)
    .setTitle("Really Exit?")
    .setMessage("Are you sure you want to exit?")
    .setNegativeButton(android.R.string.no, null)
    .setPositiveButton(android.R.string.yes, new OnClickListener() {

        public void onClick(DialogInterface arg0, int arg1) {
            WelcomeActivity.super.onBackPressed();
        }
    }).create().show();
}

I hope this may help you..

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