13

I came across the ViewPager and the related FragmentPagerAdaptor which are not part of the regular Android packages, but only available in the V4 support package. It seems I cannot use the FragmentPagerAdaptor with a android.app.Fragment since the FragmentPagerAdaptor requires a v4 FragmentManager to instantiate. Reading Difference between Activity and FragmentActivity this seems to be aligned - use either v4 Fragments and related classes or the regular android.app.Fragments. Never mix.

Taking it further, I see no need to use the regular android.app.Fragments at all since they are less powerful than the v4 support packages. (Cite from another question: The Android Support package is not only for backports of newer APIs. It is also for other classes that, for whatever reason, are not being added to the SDK, such as ViewPager and its supporting classes.). The "only" downside I can think off is that the v4 support libraries is bundled with the APK which means my app will take up more space.

Am I correct to conclude that I should always use v4 support libraries for fragments since those include more functionality? (And they are backwards compatible, not to forget.)

Community
  • 1
  • 1
TommyTh
  • 1,102
  • 9
  • 13

2 Answers2

9

I just realized that it is possible to use the android.app.Fragment and the android.support.v4.view.ViewPager together... If you use the android.support.v13.app.FragmentPagerAdapter... So it seems that the v13 support package solves/improves the compatibility issues between android.app.Fragment and android.support.v4.view.ViewPager. What a mess.

While this resolves the particular example I came up with, I am still wondering if the best option is to always use v4 classes when they are available instead of the build-in classes (e.g. android.app.Fragment) even if I am only worried about ICS and newer devices?

DeltaCap019
  • 6,532
  • 3
  • 48
  • 70
TommyTh
  • 1,102
  • 9
  • 13
  • If you're only worried about ICS and above, use the newer fragments. The backwards compatible ones are limited since they're made to support older builds. Also, if you've answered your own question then be sure to accept your answer so that others can benefit from your information. – Michael Celey Feb 14 '13 at 15:01
  • 2
    I heard many times from Google Engineers (@IO+DevBytes) that we should use the compat library even when we have the minSdk at 14 (Android 4.0) or above. Why? Because they can update the support library more often than the Android OS gets updated on devices. (comment edited after @Intrications pointed out the wrong version number - thx) – Zsolt Safrany Nov 02 '13 at 19:05
  • @ZsoltSafrany, do you have a link to any of those specific DevBytes or IO snippets? – yiati Jan 30 '15 at 15:43
0

In terms of backwards compatibility, indeed using the v4 support package version is the best approach.

As the shift of devices moves towards the operating systems that have the functionality built in it will probably become easier to use the SDK version, as all of the functionality found in the support package will be included in the SDK.

For now, stick with v4 to offer support for 2.2/2.3 devices which still hold a large share of the Android device pie

biddulph.r
  • 5,226
  • 3
  • 32
  • 47
  • 4
    I agree that it is a huge benefit that v4 provides support for legacy devices. However the question is the opposite. Even if everybody in the world had ICS based (or newer) devices, I cannot see why I should use the android.app.Fragment instead of the V4 Fragment version. The reason is that the v4 support library contains more functionality. – TommyTh Oct 25 '12 at 10:09