178

What is the difference between android.app.Fragment and android.support.v4.app.Fragment, and what are the circumstances in which each should be used?

CJBS
  • 15,147
  • 6
  • 86
  • 135
ray
  • 4,210
  • 8
  • 35
  • 46

6 Answers6

292

android.support.v4.app.Fragment is the Fragment class in the android support library, which is a compatibility package that allows you to use some of the newer features of Android on older versions of Android.

android.app.Fragment is the Fragment class in the native version of the Android SDK. It was introduced in Android 3 (API 11).

If you want to make your app use fragments, and want to target devices before API 11, you must use android.support.v4.app.Fragment. However, if you're only targeting devices running API 11 or above, you can use android.app.Fragment.

Edit: the OS-contained android.app.Fragment is now deprecated (as of API level 28), and everyone should move to using the support library implementations.

zsmb13
  • 85,752
  • 11
  • 221
  • 226
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • 4
    Also see "Using FragmentStatePagerAdapter WITHOUT support library": http://stackoverflow.com/questions/18512294/using-fragmentstatepageradapter-without-support-library – CJBS Aug 01 '14 at 20:30
  • If you override onAttach(), please see this thread to avoid it not being called under some circumstances with android.app.Fragment: https://stackoverflow.com/questions/32083053/android-fragment-onattach-deprecated – Hong Jul 26 '17 at 22:11
  • 3
    will be using Androidx : androidx.fragment.app.Fragment – Abhijit Kurane Apr 02 '19 at 10:05
57

As of 2018:

From android.app.Fragment documentation:

This class was deprecated in API level 28.
Use the Support Library Fragment for consistent behavior across all devices and access to Lifecycle.

So support fragments (android.support.v4.app.Fragment) should be used everywhere instead of native fragments(android.app.Fragment) now.

Artyom
  • 1,165
  • 14
  • 22
  • @Artyom, as a noob to android development, I expected that when the `android.app.Fragment` class was deprecated, that API level 28 would have an alternate, possibly radical replacement approach. Is there a forward approach or is this backwards compatible `android.support.v4.app.Fragment` the proper approach? – ergohack Sep 11 '18 at 16:04
  • 1
    @ergohack See it that way: for java code you can either include the bytecode inside the runtime of the device or inside the jar file of the application. Support library code will be put in your jar, and provide the same feature as the runtime-provided code, so there's no reason not to use the modern and maintained support library instead of the ageing and unmaintained code of your target phone. – Pierre May 24 '20 at 16:41
12

I use android.support.v4.app.Fragment exclusively.

All the apps I write need to support right back to Android 2.3 and this is the easiest way to do it.

If you're supporting 11+ then stick to android.app.Fragment.

alex
  • 6,359
  • 1
  • 23
  • 21
  • 18
    In that case you're missing out on a lot of features that exists in the support library but only in the native one after a certain API level. getChildFragmentManager() for example is not available until level 17. My advice is to only use the support Fragments and pretend that the native one does not exist. – Johan Aug 06 '15 at 09:09
4

If your application is targeted for API 11 or above, You can use android.app.Fragment and your APK file will be smaller.

Otherwise, add the android.support.v4.app.Fragment library to your project in order to support older android API versions (Android 3.x).

AhmadReza Payan
  • 2,171
  • 1
  • 23
  • 33
2

android.support.v4.app.Fragment is a library that you can use to get backwards-compatibility for older API version.

Fragments were added on API level 11 (along with other features) you should include that library to extend those function to pre-API 11 devices. That is a useful library and I suggest having a look at ActionBarSherlock, which extends the action bar to pre-API v11 devices.

CJBS
  • 15,147
  • 6
  • 86
  • 135
MonkeyDroid
  • 637
  • 7
  • 14
-2

If your application is targeted for API 11 or above level, You can use android.app.Fragment and it will reduce the APK size. Otherwise use android.support.v4.app.Fragment