Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment Transaction not working when using AppCompatActivity or FragmentActivity

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!

like image 613
Yokich Avatar asked Dec 19 '22 03:12

Yokich


1 Answers

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

like image 51
Henrik Gyllensvärd Avatar answered Dec 27 '22 11:12

Henrik Gyllensvärd