Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Amal Abdull
Amal Abdull
1,629 Points

In Fragment on back button

Hi , I Have Fragments , let assume a,b,c,d

c-> is list RecyclerView

d is fragment and can be called from a


through b -> c -> d

the problem now when d called from a , d back button not working

and d through b -> c -> d
d back not working d back back return to b

here how i start fragment

fragmentManager.beginTransaction().replace(R.id.content_frame,frag) .addToBackStack(null) .commit();

and here is onBackPressed()

@Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { if (getFragmentManager().getBackStackEntryCount() > 0) { getFragmentManager().popBackStack(); } else { super.onBackPressed(); } } }

1 Answer