4

I have a typical application. An activity which has a FrameLayout and in this layout I want to switch between fragments. This is typically and easily done with:

getFragmentManager().beginTransaction()
                .replace(R.id.ac_container, new FrOverview())
                .addToBackStack(null)
                .commit();

The problem is, that even if I use .addToBackStack(null) (And I know it's been added 'cause the stack count increases) when I press back I exit the application. I have been trying a lot of different code stuff and checked most threads here on Stackoverflow but I can't get it to work with code (method calls etc.).

But! I can get it to work, by changing the extended class of my activity class. If my class extends Activity, it works fine. But if I use AppCompatActivity (which in turn extends FragmentActivity) then it has the bad behaviour as explained earlier.

Feels like this has to be an error on Androids part, I am not doing anything wrong to my knowledge.

Does anyone have any suggestions on how to solve this? i.e. get the back functionality and keep the ActionBar!

Yokich
  • 1,247
  • 1
  • 17
  • 31
  • 1
    use `getSupportedFragmentManager` to make fragment transaction and change your fragments extends to `android.support.v4.app.Fragment` – Pravin Jun 03 '15 at 12:50
  • FrOverview() is of android.support.v4.app.Fragment type or android.app.Fragment type. – Akash Jun 03 '15 at 12:56

1 Answers1

11

AppCompatActivity uses the SupportFragmentManager, you need to switch to SupportFragment and SupportFragmentManager

Yokich
  • 1,247
  • 1
  • 17
  • 31
  • Which then in turn needs me to change the `Fragments` to `android.support.v4.app.Fragment` from the support Library ;) Thanks! – Yokich Jun 04 '15 at 07:12