8

I have a HomeFragment that has a button, which when clicked calls the following:

Fragment frag = new CustFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.home_container, frag).commit();

Then in my FragmentActivity which is the fragments mentioned above, I have:

@Override
public void onBackPressed() {

    getFragmentManager().popBackStack();
    super.onBackPressed();

}

That's what I've tried, but if I'm on the frag fragment and I press the back button, it doesn't go back to the last fragment (the HomeFragment). Instead, it attempts to go back to the last Activity, but since there is none (i.e. the previous activity had finish() invoked on it), it just goes to the Android Home Screen.

What am I doing wrong?

PS: If I'm being unclear, just comment below and i'll try to clarify.

u3l
  • 3,342
  • 4
  • 34
  • 51

4 Answers4

11

Change

@Override
public void onBackPressed() 
{
   getFragmentManager().popBackStack();
   super.onBackPressed();
}

to

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

and

fragmentManager.beginTransaction().replace(R.id.home_container, frag).commit();

to

fragmentManager.beginTransaction().replace(R.id.home_container, frag).addToBackStack(null).commit();
Kamil Z
  • 181
  • 12
Apoorv
  • 13,470
  • 4
  • 27
  • 33
  • 1
    OnBackPressed is not needed if you use addToBackStack – Kuno Jun 30 '14 at 10:23
  • 1
    @KlassenK If he doesn't do that `super.onBackPressed();` will directly exit the `Activity` – Apoorv Jun 30 '14 at 10:24
  • Try to add "addToBackStack(null)" to your transaction like I linked in my answer and remove onBackPressed completely. – Kuno Jun 30 '14 at 10:28
3

Add your fragment to back stack using addToBackStack(null) like..

fragmentManager.beginTransaction().replace(R.id.home_container, frag).addToBackStack(null).commit();
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
1

beginTransaction creates a new FragmentTransaction. It has a method addToBackstack. If you call it before you commit your transaction you can remove your overridden onBackPressed completely. Reference

Kuno
  • 3,492
  • 2
  • 28
  • 44
1

You can use:

fragmentTransaction.addToBackStack(null);

and no need to take care of onBackPressed().

BTW in your onBackPressed() super.onBackPressed() means you are actually changing nothing.

It should have been something like:

if(currentFragmentIsCustFragment){
   getSupportFragmentManager().popBackStack();
}else{
   super.onBackPressed();
}
Kamil Z
  • 181
  • 12
Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
  • Ohh okay. Wait, how come I don't have to take care of `onBackPressed`? Will it pop off the stack automatically if I press the back button without me having to implement it? – u3l Jun 30 '14 at 10:23
  • Yeah, that is taken care by `fragmentTransaction.addToBackStack(null);` – Archie.bpgc Jun 30 '14 at 10:24