2

Firstly, I am aware of the existence of this highly voted question -

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

However, the answers here discusses more on when to use them (i.e. support Fragments for API 4+ and app Fragments for API 11+) than what are the differences in their behaviors and usages.

I'd like to know what are the advantages or disadvantages of using either when developing for a min SDK where both of them are supported (e.g. 16+) and what are the new features supported in app Fragments and not in support Fragments.

Community
  • 1
  • 1
jaibatrik
  • 6,770
  • 9
  • 33
  • 62
  • 4
    "what are the new features supported in app Fragments and not in support Fragments" -- AFAIK, there are none. If anything, it is the inverse: the `support-v4` fragments will have things that might not exist in native fragments on older API levels. For example, nested fragments were added to native fragments starting with API Level 17. if you need nested fragments (and heaven help you if you do), with a `minSdkVersion` of 16, you would need to use the `support-v4` fragment implementation. – CommonsWare Jul 02 '16 at 20:30

1 Answers1

1

I can't tell you the logic behind each of Google's changes, but per Google documentation at https://developer.android.com/topic/libraries/support-library/packages#v4-fragment:

The v13 support library provides a FragmentCompat class. The v4 Fragment class is a standalone class that provides bugfixes which were added in later platform versions, whereas the v13 FragmentCompat class provides compatibility shims for the framework implementation of the Fragment class.

So to summarize you are basically getting bug fixes by using the support version.

At https://developer.android.com/reference/android/support/v4/app/Fragment it says:

The main differences when using this support version instead of the framework version are:

  • Your activity must extend FragmentActivity
  • You must call getSupportFragmentManager() to get the FragmentManager

Using an online diff tool I can see the following differences in the API (not exhaustive) as of support library 28.0.0-alpha1 vs framework API level 28:

  • In the support version getActivity returns a FragmentActivity instead of an Activity and the support version docs explain that the function may return null if the fragment is associated with a context
  • Functions which operate on transitions such as getReenterTransition return Object in the support version instead of ObjectTransition
  • The support version added the following functions: getLifecycle, getViewModelStore, onCreateAnimation, requireActivity, requireContext, requireFragmentManager, requireHost
  • The onInflate method which takes an Activity parameter is deprecated

I see no notable difference in FragmentManager or FragmentTransaction to speak of other than:

  • In the support version setAllowOptimization is deprecated and the authors say setReorderingAllowed is the replacement

To summarize the only major difference I see is that it appears that the support Fragment need not be associated with an Activity but the framework Fragment seems to require an associated Activity to function properly.

satur9nine
  • 13,927
  • 5
  • 80
  • 123