Difference between Fragment and FragmentActivity

6

Well, the title is pretty clear, what is the difference between Fragment and FragmentActivity?

I would like to know to know which is the best option for the following case: I have two fragments and in a main activity (with a NavigationView) in which a fragment or another will be loaded (both at the same time, not now).

I have also thought about using a ViewPager to manage the fragments, which is used quite simply and saves the data if I turn the screen and do not have to be controlling it myself, but the problem with that is that it loads me the two fragments at the beginning of the activity (or so I think) and also allows you to change the fragments by sliding your finger to one side and another.

    
asked by borjis 29.11.2016 в 17:23
source

4 answers

11

Fragment as an Activity contains its own Lifecycle and represents a behavior or part of the user interface in a Activity , the most important thing is that it can be added or removed from the Activity that contains it, it is always attached to a Activity which requires you to work.

FragmentActivity is a Activity that contains support for Fragments, which gives us the possibility to make Fragments transactions within it, that is, add or delete.

The main difference between FragmentActivity and Fragment is the Fragment is a component that represents a behavior or part of the user interface in a Activity , but you need a Activity to display; and the FragmentActivity is a Activity as we know it but that 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 a Fragment :

There is an answer in 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:

  • Your own life cycle
  • Receive your own input events
  • Can be added or removed while the activity is running.
  • A Fragment must always be embedded in an Activity.

The fragments are not part of the API before HoneyComb (Android 3.0). If you want to use Fragments in an application oriented to a platform version prior to HoneyComb, you must add the support package to your project and use FragmentActivity to store your fragments.

The class FragmentActivity has an API to deal with fragments, while the class Activity before Android 3.0 it did not contain it.

link

    
answered by 29.11.2016 / 17:39
source
2

Apparently there is a difference at the level of concepts but not of functionality. FragmentActivity is worked in previous versions in Android, when you use android.app.Fragment and so that no compatibility exceptions are generated, FragmentActivity is used, while if you use android.support.v4.app.Fragment corresponds to Fragment , both are identical, they have the same functions although they change a little name, in FragmentActivity the function getLoaderManager() and getFragmentManager() change to getSupportLoaderManager() and getSupportFragmentManager() respectively. But both remain a subclase of Activity

    
answered by 29.11.2016 в 17:30
2

I found a similar question in the StackOverflow forum in English repondida by @MrK that I think is very well explained and she has a lot of positive votes, so I decided to translate it because it fits what you ask.

Translation:

A Fragment is a section of a Activity , which has:

  • Your own life cycle
  • Receive your own input events
  • Can be added or removed while Activity is running.

A Fragment should always be embedded in a Activity .

Fragments are not part of the API before HoneyComb (3.0). If you want to use Fragments in an application targeting a platform version before HoneyComb, you must add the Support Package to your project and use FragmentActivity to save your Fragments . Class FragmentActivity has an API to deal with Fragments , while class Activity , before HoneyComb, no.

If your project is targeting HoneyComb or newer only, you should use Activity and not FragmentActivity to embed your Fragments .

Some details:

Use android.app.Fragment with Activity . Use android.support.v4.app.Fragment with FragmentActivity . Do not add the support package Fragment to Activity , since it will cause an exception to be thrown.

One thing to be careful with: FragmentManager and LoaderManager have separate support versions for FragmentActivity:

If you are using a Fragment in a Activity (HoneyComb and above), call:

  • getFragmentManager() to get android.app.FragmentManager
  • getLoaderManager() to get android.app.LoaderManager

If you are using a Fragment in a FragmentActivity (pre-HoneyComb), call:

  • getSupportFragmentManager() to get android.support.v4.app.FragmentManager .
  • getSupportLoaderManager() to get android.support.v4.app.LoaderManager

Therefore, do not do

//don't do this
myFragmentActivity.getLoaderManager(); 
//instead do this:
myFragmentActivity.getSupportLoaderManager();

or

//don't do this:
android.app.FragmentManager fm = myFragmentActivity.getSupportFragmentManager();
//instead do this:
android.support.v4.app.FragmentManager fm = myFragmentActivity.getSupportFragmentManager()

It is also useful to know that while a fragment has to be embedded in a Activity it does not have to be part of the layout Activity . It can be used as an invisible worker for the activity, without its own UI.

    
answered by 29.11.2016 в 17:41
1

A FragmentActivity is a Activity that can support Fragments .

However, a Fragment on its own always has to depend on a Activity . You could say it's how a section of Activity .

    
answered by 29.11.2016 в 17:28