164

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...

nbro
  • 15,395
  • 32
  • 113
  • 196
D-Dᴙum
  • 7,689
  • 8
  • 58
  • 97

4 Answers4

289

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.

Paritosh
  • 2,097
  • 3
  • 30
  • 42
Gunnar Karlsson
  • 28,350
  • 10
  • 68
  • 71
  • 7
    However, a fragment is not required to be a part of the activity layout; you may also use a fragment without its own UI as an invisible worker for the activity. – uzay95 Mar 12 '13 at 08:56
  • 1
    @uzay95 thanks for pointing this out. I added a section at the end to mention it. – Gunnar Karlsson Mar 12 '13 at 10:14
  • @GunnarKarlsson Shall we add fragment to fragmentActivity? – Dhasneem May 25 '13 at 07:37
  • 2
    @Dhasneem You add a Fragment to a FragmentActivity if you want your app to work on Android versions lower than 3.0. Otherwise you add a Fragment to an Activity. – Gunnar Karlsson May 25 '13 at 23:16
  • 2
    Thanks! This solve my problem! Now I know why my app (written with Activity) always crash at onCreate after adding Facebook login button (they use android.support.v4.app.Fragment in their example). Of course there are also other reason for the crash. But your answer is the final piece to put them together. – Chlind Jan 21 '14 at 18:25
  • 1
    update (sep '18): since this post comes up first when you google "fragment vs fragment activity", i want to mention and developer.android.com has declared the "newer" fragment\fragment-manager as deprecated, and redirect users to in fact use the backwards compatible support library (fragment-Activity\support-fragment-manager). I am still trying to figure out that matter, but i thought it was worth mentioning – yoad w Sep 28 '18 at 11:16
15

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

Gökhan Barış Aker
  • 4,445
  • 5
  • 25
  • 35
  • 1
    BTW, Out-of-date answer, which only applies to apps that wish to work on devices older than API 11. – ToolmakerSteve Sep 21 '15 at 12:37
  • @ToolmakerSteve can you provide more details? If possible, can you edit the answer with up-to-date version? – Gökhan Barış Aker Sep 22 '15 at 09:02
  • @ToolmakerSteve I don't see how this answer is outdated? Everything he said still stands true in 2022. – M.Ed Jun 18 '22 at 17:17
  • IIRC, this is outdated because the `Activity` class now has the necessary Fragment-related functionality, that originally only existed in `FragmentActivity`. As accepted answer says *"The FragmentActivity class has an API for dealing with Fragments, whereas the Activity class, **prior to HoneyComb, doesn't**."* – ToolmakerSteve Jun 20 '22 at 18:30
8

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.

j2emanue
  • 60,549
  • 65
  • 286
  • 456
1

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.

Simone Casagranda
  • 1,217
  • 17
  • 26