Fragment or Support Fragment?

AndroidAndroid FragmentsAndroid Support-Library

Android Problem Overview


I am developing an app that supports Android >= 4.0. It uses fragments from the android.app package. As I am facing problems with the older fragment implementation in 4.0, like this one, that are already fixed in the support library, I am considering switching back to the fragment implementation from the support library to get a more reliable and consistent implementation.

What is your opinion on this? Are you using fragments from the support library, even though they are already available, when developing for Android 4?

Android Solutions


Solution 1 - Android

From my experience, using the same fragment implementation on all Android devices is a great advantage. I could not get rid of all NullPointerExceptions when state is saved on Android 4.0 using native fragments, with the support library they are all gone. Also I could not see any disadvantage so far with this approach.

So my answer to my own question is now: When developing for Android 4.x, using the fragments from the support library is a good idea. The support library has bugs fixed that are still present in older fragment implementations and is frequently updated with more bug fixes.

Solution 2 - Android

One big reason to stick with the SupportFragment for a while is that you do not have access to the ChildFragmentManager until API 17. The support library will give you a support version of the child fragment manager.

This becomes a big deal if you have fragments that contain other fragments. This is common in tablet applications with a good deal of complexity and/or your overall architecture is based on either a tabbed layout or uses the navigation drawer.

Solution 3 - Android

I was also getting frustrated at having to include the support libraries, despite targeting Android 4.0+ - but it seems it is officially recommended:

> The Android Support Library package contains several libraries that > can be included in your application. Each of these libraries supports > a specific range of Android platform versions and set of features. > > This guide explains the important features and version support > provided by the Support Libraries to help you decide which of them you > should include in your application. In general, we recommend including > the v4 support and v7 appcompat libraries, because they support a wide > range of Android versions and provide APIs for recommended user > interface patterns.

http://developer.android.com/tools/support-library/features.html

Solution 4 - Android

IMHO if you are planning to develop for 4.0 only, I would recommend going with the native libraries since the executable will get smaller. It is true that you might run into problems of bugs in early versions, but I think most of these should be fairly trivial to work around. Also the compatibility library is supposed to map to the native fragments in case you are running on 4.0 and higher anyway. So you might end up having to struggle with these kinds of problems anyway. The problem with the support libraries is that you have a lot of the classes appear 2x (once in the support package structure and once in the "native" package structure) which makes development a bit more cumbersome.

However, if you want to also release your app pre 4.0 then there is no way around the support library. Also since there are about 38% of all users on 2.3 it might make business sense to include this OS version. In such a case you can use the support library in combination with Jake Wartons ActionBarSherlock (or with googles support ActionBar Library once it is finally released).

Solution 5 - Android

It seems that it is better to use Support Library now because I saw the statement here https://developer.android.com/reference/android/app/Fragment.html

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

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
QuestionbrillenheiniView Question on Stackoverflow
Solution 1 - AndroidbrillenheiniView Answer on Stackoverflow
Solution 2 - AndroidRoss HambrickView Answer on Stackoverflow
Solution 3 - AndroidsourabhjView Answer on Stackoverflow
Solution 4 - AndroidRaBView Answer on Stackoverflow
Solution 5 - Androidcode4jView Answer on Stackoverflow