I was working on fragments and came across two things Activity
and FragmentActivity
which are used several times. I want to know that is there any difference between these two, because when I changed Activity
with FragmentActivity
, it had no effect on the app.
A FragmentActivity
is a subclass of Activity
that was built for the Android Support Package.
The FragmentActivity
class adds a couple new methods to ensure compatibility with older versions of Android, but other than that, there really isn't much of a difference between the two. Just make sure you change all calls to getLoaderManager()
and getFragmentManager()
to getSupportLoaderManager()
and getSupportFragmentManager()
respectively.
FragmentActivity
is part of the support library, while Activity
is the framework's default class. They are functionally equivalent.
You should always use FragmentActivity
and android.support.v4.app.Fragment
instead of the platform default Activity
and android.app.Fragment
classes. Using the platform defaults mean that you are relying on whatever implementation of fragments is used in the device you are running on. These are often multiple years old, and contain bugs that have since been fixed in the support library.
minSdkVersion
of support library is 14 since version 26.x.x.
Success story sharing
FragmentActivity
inherits thegetLoaderManager
andgetFragmentManager
methods fromActivity
and as a result the compiler won't complain. Chances are you are importing the incorrectLoaderManager
andFragmentManager
classes too. Make sure you are importing these classes from the support package (android.support.v4.app
), not the Android SDK (android.app
).Activity
if you are usingandroid.app.Fragment
; useFragmentActivity
if you are usingandroid.support.v4.app.Fragment
. Never attach aandroid.support.v4.app.Fragment
to aandroid.app.Activity
, as this will cause an exception to be thrown.Fragments
in an application). Then, continue on to this tutorial (how to make use of theFragment
s from the support package). The documentation on the developers site is worth reading as well.