Difference between android.app.Fragment and android.support.v4.app.Fragment

AndroidAndroid Fragments

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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.

Solution 3 - Android

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.

Solution 4 - Android

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

Solution 5 - Android

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.

Solution 6 - Android

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

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
QuestionrayView Question on Stackoverflow
Solution 1 - AndroidRaghav SoodView Answer on Stackoverflow
Solution 2 - AndroidArtyomView Answer on Stackoverflow
Solution 3 - AndroidalexView Answer on Stackoverflow
Solution 4 - AndroidAhmad PayanView Answer on Stackoverflow
Solution 5 - AndroidMonkeyDroidView Answer on Stackoverflow
Solution 6 - Androidvinod wickramasekaraView Answer on Stackoverflow