What is the difference between Fragment and FragmentActivity?

AndroidAndroid FragmentsAndroid Fragmentactivity

Android Problem Overview


My question is apart from the obvious inheritance differences, what are the main differences between Fragment and FragmentActivity? To what scenarios are each class best suited? I'm trying to get an understanding of why both of these classes exist...

Android Solutions


Solution 1 - Android

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

  • its own lifecycle
  • receives its own input events
  • can be added or removed while the Activity is running.

A Fragment must always be embedded in an Activity.

Fragments are not part of the API prior to HoneyComb (3.0). If you want to use Fragments in an app targeting a platform version prior to HoneyComb, you need to add the Support Package to your project and use the FragmentActivity to hold your Fragments. The FragmentActivity class has an API for dealing with Fragments, whereas the Activity class, prior to HoneyComb, doesn't.

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

Some details:

Use android.app.Fragment with Activity. Use android.support.v4.app.Fragment with FragmentActivity. Don't add the support package Fragment to an Activity as it will cause an Exception to be thrown.

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

If you are using a Fragment in an Activity (HoneyComb and up), 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

so, don't 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()

Also useful to know is that while a fragment has to be embedded in an Activity it doesn't have to be part of the Activity layout. It can be used as an invisible worker for the activity, with no UI of its own.

Solution 2 - Android

FragmentActivity is our classic Activity with fragment support, nothing more. Therefore FragmentActivity is needed, when a Fragment will be attached to Activity.

Well Fragment is good component that copy the basic behaviors of Activity, still not a stand-alone application component like Activity and needs to be attached to Activity in order to work.

Look here for more details

Solution 3 - Android

Think of FragmentActivity as a regular Activity class that can support Fragments. Prior to honeycomb, an activity class could not supoprt Fragments directly, so this is needed in activities that use Fragments.

If your target distribution is Honeycomb and beyond you can extend off of Activity instead.

Also a fragment is to be considered as a 'sub-activity'. It cannot exist without an activity. Always think of a fragment as a sub-activity and you should be good. So the activity would be the parent and the fragment(s) the child kind of symbolic relationship.

Solution 4 - Android

a FragmentActivity is an ad-hoc activity that contains Fragment. In these few words I have explain you one of the main important changes that, with android 3.0(HoneyComb), android team has inserted in the android sdk.

With these new kind of concept your pieces of code and layout becomes more flexible and maintainable. If you search on google there are a lot of examples.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionD-DᴙumView Question on Stackoverflow
Solution 1 - AndroidonosendaiView Answer on Stackoverflow
Solution 2 - AndroidGökhan Barış AkerView Answer on Stackoverflow
Solution 3 - Androidj2emanueView Answer on Stackoverflow
Solution 4 - AndroidSimone CasagrandaView Answer on Stackoverflow