Well the title is quite clear, what is the difference between Fragment and FragmentActivity?
I would like to know what is the best option for the following case: I have two fragments and in a main activity (with a NavigationView) in which one fragment or another will be loaded (not both at the same time for now).
I have also thought of using a ViewPager to manage the fragments, which is used in a fairly simple way and saves the data if I rotate the screen and I don't have to control it myself, but the problem with that is that it loads the two fragments at the same time. start of the activity (or so I think) and also allows you to change the fragments by sliding your finger back and forth.
Fragment as an Activity contains its own Lifecycle and represents a behavior or a part of the user interface in an Activity , the most important thing is that it can be added or removed from the Activity that contains it, it is always attached to an Activity the which is required to work.
FragmentActivity is an Activity that contains support for Fragments, which gives us the possibility of carrying out Fragment transactions within it, that is, adding or deleting.
The main difference between FragmentActivity and Fragment is that Fragment is a component that renders a behavior or a piece of user interface in an Activity , but it needs an Activity in which to display itself; and the FragmentActivity is an Activity as we know it but it contains support for Fragments so we can add a Fragment to it.
This would be the difference between the life cycle of a
Activity
and aFragment
:There is an answer on the SO site in English, answered by @MrK that refers to your question: "Difference between Fragment and FragmentActivity"
A Fragment is a section of an Activity, which has:
Fragments are not part of the API before HoneyComb (Android 3.0). If you want to use Fragments in an application targeting a platform version earlier than HoneyComb, you must add the support package to your project and use a FragmentActivity to store your fragments.
The class
FragmentActivity
has an API for dealing with fragments, whereas the Activity class before Android 3.0 did not.https://stackoverflow.com/a/10609839/250260
Apparently there is a difference at the level of concepts but not functionality.
FragmentActivity
is worked in previous versions in Android, when you useandroid.app.Fragment
and so that compatibility exceptions are not generated, it is usedFragmentActivity
, while if you use itandroid.support.v4.app.Fragment
corresponds to occupyFragment
, both are identical, they have the same functions although they change a little in name, inFragmentActivity
the functiongetLoaderManager()
andgetFragmentManager()
they change togetSupportLoaderManager()
andgetSupportFragmentManager()
respectively. But both are still onesubclase
ofActivity
I have found a similar question in the StackOverflow forum in English answered by @MrK that I think is very well explained and has quite a few positive votes, so I have decided to translate it since it conforms to what you are asking.
Translation:
A
Fragment
is a section of aActivity
, which has:Activity
.A
Fragment
must always be embedded in aActivity
.Fragments
they are not part of the API prior to HoneyComb (3.0). If you want to useFragments
in an application targeting a platform version before HoneyComb, you must add the Support Package to your project and useFragmentActivity
to save yourFragments
. The classFragmentActivity
has an API to deal withFragments
, whereas the classActivity
, before HoneyComb, did not.If your project targets HoneyComb or newer only, you should use
Activity
and notFragmentActivity
to embed yourFragments
.Some details:
Use
android.app.Fragment
withActivity
. Useandroid.support.v4.app.Fragment
withFragmentActivity
. Do not add the support packageFragment
toActivity
, as this will cause an exception to be thrown.One thing to watch out for:
FragmentManager
andLoaderManager
they have separate support releases for FragmentActivity:If you're using one
Fragment
at aActivity
time (HoneyComb and above), call:getFragmentManager()
to getandroid.app.FragmentManager
getLoaderManager()
to getandroid.app.LoaderManager
If you're using a
Fragment
in aFragmentActivity
(pre-HoneyComb), call:getSupportFragmentManager()
to getandroid.support.v4.app.FragmentManager
.getSupportLoaderManager()
to getandroid.support.v4.app.LoaderManager
So do n't do
either
It's also useful to know that while a fragment has to be embedded in a
Activity
layout it doesn't have to be part of the layoutActivity
. It can be used as an invisible worker for the activity, with no UI of its own.A
FragmentActivity
is oneActivity
that can bearFragments
.However a
Fragment
business alone always has to rely on a businessActivity
. You could say that it is like a section of theActivity
.