Android bottom navigation bar when press back go to previous fragment-ThrowExceptions

Exception or error:

Here is my navigation bar :

Screenshot of 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!

How to solve:

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

###

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..

Leave a Reply

Your email address will not be published. Required fields are marked *