Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment or Support Fragment?

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?

like image 727
brillenheini Avatar asked Oct 04 '22 03:10

brillenheini


People also ask

What is a fragment and why is it used?

A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.


2 Answers

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.

like image 102
brillenheini Avatar answered Oct 11 '22 07:10

brillenheini


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.

like image 38
Ross Hambrick Avatar answered Oct 11 '22 08:10

Ross Hambrick