Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

addToBackstack not working when replacing fragments

I am having an issue where I call addToBackStack on my fragment when replacing it, but when I press back to go back to that fragment, it doesn't go back, it just closes my app.

Fragment fragmentWebView = new MyWebView();
transaction.replace(R.id.content_frame, fragmentWebView);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();

Am I doing anything wrong here? everything looks fine to me.

like image 954
Jack Avatar asked Jul 23 '26 06:07

Jack


1 Answers

Try to add this code to your activity

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0 ){
        getFragmentManager().popBackStack();
    } else {
        super.onBackPressed();
    }
}

Watched here

Hope it helps

like image 129
jos Avatar answered Jul 25 '26 20:07

jos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!