Erhannis
Erhannis

Reputation: 4462

AppCompatActivity design

Why doesn't AppCompatActivity's getFragmentManager() return a support version of the fragment manager? Instead you have to both make your Activity an AppCompatActivity AND specifically call getSupportFragmentManager(). The same is true for a dozen other methods. It bugs me that you have to magically know which methods you can call as themselves, and which you have to rebrand as the "support" version. Is there even any use case for wanting to call the "normal" version instead of the "support" version, when you're still supporting lower versions of Android? It'd make a lot more sense to me if AppCompatActivity functioned indistinguishably from the later API versions of Activity - after all, that's what it's SUPPOSED to do, isn't it? Provide the same functionality as the later API versions of Activity?

Is there some kind of design principle or hidden Java limitation that prevents them from doing so?

Upvotes: 0

Views: 68

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006614

Why doesn't AppCompatActivity's getFragmentManager() return a support version of the fragment manager?

Because it can't.

Activity, on API Level 11+, defines getFragmentManager() as returning an android.app.FragmentManager. The only way that AppCompatActivity could override it is if the overridden method also returns an android.app.FragmentManager. That's not possible. AppCompatActivity is designed to work back to API Level 7, where there is no android.app.FragmentManager to return. Hence, we have getSupportFragmentManager(), returning an android.support.v4.app.FragmentManager.

Note that AppCompactActivity really gets its fragments from FragmentActivity, which is designed to work back to API Level 4, two years before API Level 11 and native fragments were implemented.

Upvotes: 1

Related Questions