3

I am making an app which should have minSdk = 15 and targetSdk = 21, therefore I want to use the features provided by the appcompat-v7 library.

I always wondered if I should use getFragmentManager or getSupportFragmentManager when using the supportlibrary-v7.

I am encountering a small problem now: when using getFragmentManager (and therefore using the framework fragments and fragmenttransaction) I wasnt able to pop the backstack by simply pressing the backbutton - I had to do a backStackCount > 0 check and manually popBackStack, otherwise my activity was simply finished. This problem was solved when I switched my small app to use the v4 classes (getSupportFragmentManager etc.). Which is fine I guess, but I would like to have a guideline/bestpractice to know which way to go and why

So, my Activity is inheriting from ActionBarActivity (according to AppCompat-Blog-Entry) and I am using the new toolbar, should I use only v4-Fragments(-Manager, -Transactions)?

I havent found any best practices or guidlines for that. And I am unsure about what to consider when deciding between these two :-/

degill
  • 1,285
  • 2
  • 13
  • 19
  • I had the same problem in my applications and the answer is: use the classes that libraries provide about Fragments. Because there are some classes like [FragmentPagerAdapter](http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html) that you can only use with support libraries so it is wise to use all the classes that Support Libraries related to Fragments. – eluleci Jan 08 '15 at 10:32
  • 1
    Actually there is a [v13.FragmentPagerAdapter](https://developer.android.com/reference/android/support/v13/app/FragmentPagerAdapter.html) which works with the framework Fragments. But still, this is not really a guideline IMO. As I said, I totally switched to v4 for consistency reasons. – degill Jan 08 '15 at 10:58

1 Answers1

5

If you are inheriting your activities from ActionBarActivity you should always use getSupportFragmentManager(). It automatically forwards your calls to getFragmentManager() if the phone supports it (runs Honeycomb or later), otherwise it uses its compatibility implementation.

stealth
  • 371
  • 2
  • 8
  • I use the appcompat-v7 not only because of Fragment support (I know that Fragment support was added prior to API 15). The main reason is because I want to use some features introduced in API 21, like the Toolbar or the MaterialDesign-Theme. So AFAIK I need to use the appcompat-v7. – degill Jan 08 '15 at 13:55
  • In this case go for the `ActionBarActivity`. If you plan to use Toolbar you will quite likely need the `setActionBar(...)` method which is not available before API 21 - use `setSupportActionBar(...)` instead. – stealth Jan 08 '15 at 14:06
  • You are right, I need to use the `setSupportActionBar()` Method. So, basically, because of my usage of the AppCompat, I should use all the support methods? – degill Jan 08 '15 at 17:51
  • Thanks for your answers. Still this is not very satisfying because I still dont know why and when one need to use the support functions. I would accept your answer, but the last paragraph is wrong IMO. – degill Jan 09 '15 at 10:25
  • 1
    For future reference: The support fragment manager within FragmentActivity does not forward any calls to the framework fragment manager. This can be seen in the sources (https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v4/java/android/support/v4/app/FragmentManager.java) and is stated in the docs: "When running on Android 3.0 or above, this implementation is still used; it does not try to switch to the framework's implementation." (https://developer.android.com/reference/android/support/v4/app/FragmentManager.html) – degill Mar 15 '16 at 13:57
  • Just use the regular fragment manager if you're using AppCompatActivity and targeting anything recent. The supportFragmentManager is only for backporting fragments to earlier apis. – dcow Mar 25 '17 at 22:15