248

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.

Lachlan Goodhew-Cook
  • 1,101
  • 17
  • 31
Rookie
  • 8,660
  • 17
  • 58
  • 91

2 Answers2

338

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.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • 4
    Hey I didn't change the calls to getSupportLoaderManager() and getSupportFragmentManager()..but still it is working fine. – Rookie May 07 '12 at 07:28
  • 33
    That's what makes it so dangerous :P. `FragmentActivity` inherits the `getLoaderManager` and `getFragmentManager` methods from `Activity` and as a result the compiler won't complain. Chances are you are importing the incorrect `LoaderManager` and `FragmentManager` classes too. Make sure you are importing these classes from the support package (`android.support.v4.app`), *not* the Android SDK (`android.app`). – Alex Lockwood May 07 '12 at 07:40
  • 1
    Can we say that if we want to use fragments for android 2.x version we need to use FragmentActivity nad for version that support fragments we need to use Activity . – Rookie May 07 '12 at 08:31
  • 60
    I think what you are saying is correct. But just to be 100% clear... use `Activity` if you are using `android.app.Fragment`; use `FragmentActivity` if you are using `android.support.v4.app.Fragment`. Never attach a `android.support.v4.app.Fragment` to a `android.app.Activity`, as this will cause an exception to be thrown. – Alex Lockwood May 07 '12 at 08:37
  • Can you suggest where could i find a good tutorial for fragments. – Rookie May 07 '12 at 08:44
  • 14
    First try [**this tutorial**](http://mobile.tutsplus.com/tutorials/android/android-sdk_fragments/) (how to use `Fragments` in an application). Then, continue on to [**this tutorial**](http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/) (how to make use of the `Fragment`s from the support package). The [**documentation**](http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html) on the developers site is worth reading as well. – Alex Lockwood May 07 '12 at 08:52
  • @AlexLockwood I am importing the correct classes i.e. `android.support.v4.app.ListFragment` and `android.support.v4.app.FragmentActivity`, but still getting the error at `getFragmentManager()`. Why doesn't it inherit `getFragmentManager()` from the `Activity` because `FragmentActivity` extends `Activity` – Solace Dec 17 '14 at 08:32
  • 1
    Maybe it is not a compilation error and it is just a lint analysis error. Either way, you should be using `getSupportFragmentManager()` in your `FragmentActivity` instead. – Alex Lockwood Dec 17 '14 at 15:17
8

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.

Samuel Peter
  • 4,136
  • 2
  • 34
  • 42