182

I have a question regarding the Android Support Libraries, Fragments, and as a specific example, the ViewPager class. My intention is to create an app with similar functionality to the sample provided on the Android Developer website (http://developer.android.com/training/animation/screen-slide.html or http://developer.android.com/training/implementing-navigation/lateral.html). Looking into their code, I've noticed they utilize the android.support.v4.app library, which from my research is the only way to access the ViewPager class.

In my situation, I have no interest in backward compatibility. The minimum API level is 14 (Ice Cream Sandwich) and the build target is 4.2 Jelly Bean. In it's simplest form, my app performs exactly as does the second demo I linked on the Android dev website - just swiping between three tabs with content in each.

All of the articles/posts/answers I've read seem to heavily favor the v4 support library. Now for my, albeit long-winded, question(s):

  1. What's the best way to structure my application - using android.support.v4.app, and thereby using SupportFragments, or to use the Fragments provided in android.app - and why?

  2. If Fragments from android.app are the way to go, what is the optimal way to approach ViewPagers?

  3. If SupportFragments are best-suited to the task, I would estimate that they possess the same functionality as the other - so what's the purpose of having them at all inside android.app?

Hopefully someone with a clearer understanding can give me a bit of clarification because I'm boggled...

Tot Zam
  • 8,406
  • 10
  • 51
  • 76
jonstaff
  • 2,672
  • 2
  • 19
  • 18
  • 1
    you cannot use `Fragments` if you are using the `ViewPager` you need to use the support library `SupportFragment` – tyczj Jul 09 '13 at 16:24
  • 1
    `SupportFragment` is not a class in the Android SDK... – Karakuri Jul 09 '13 at 16:25
  • 2
    @Karakuri Sorry for the confusion; I don't mean a literal class `SupportFragment`, I'm merely referring to the `Fragment` class available in `android.support.v4.app`. – jonstaff Jul 09 '13 at 16:39
  • @jonstaff either way you can only use the fragment class in the support library – tyczj Jul 09 '13 at 16:42
  • @tyczj Thanks, but that doesn't really answer the question. I'm looking more for a **why**, since I already know there is no `ViewPager` in `android.app`. – jonstaff Jul 09 '13 at 17:13
  • @jonstaff because ViewPager uses uses everything from the support library thats why ie. `FragmentPagerAdapter`. you cannot mix classes from the support library with the regular SDK – tyczj Jul 09 '13 at 17:21
  • Very, very, very good question, I am trying to do the same thing and wrapping my head around the question: Why do I need to switch to support fragment if my app runs 4.0 and above in order to get ViewPager running??!?! And now comes another nail in my head: v13...jeezz. – Edmond Tamas Jul 25 '15 at 09:02

3 Answers3

183

You can use ViewPager with native fragments from the android.app package with the adapters from the android.support.v13.app package. You have to use the v13 support jar for that.

There are two versions of the adapters that work with ViewPager, the ones in the v4 package are meant to be used with support fragments, the ones in v13 with native fragments.

The reason why there are now two fragment implementations is historical: Fragments in the android.app package were introduced with Android 3 for tablets only and the support library was created to bring fragments to phones running older versions. On Android 4 you have both.

From my own experience I would recommend using support fragments, even when developing for Android 4. Here are some reasons: Fragment or Support Fragment?

Community
  • 1
  • 1
brillenheini
  • 5,413
  • 3
  • 23
  • 20
  • support.v13 works for ViewPager ... until you try and use ViewPager in a DialogFragment :-( – SteelBytes Oct 07 '14 at 07:41
  • `android.app` does not have `FragmentPagerAdapter` so then it become impossible to use viewpager with android.app.Fragment? any idea here? is there any option to old view pager? So as to dynamically hook fragment for given tabs – Nikhil Nov 15 '15 at 19:26
  • @Nikhil there are two versions of FragmentPagerAdapter. The one in the `android.support.v4.app` package works with support fragments, the other in `android.support.v13.app` with 'normal' fragments – brillenheini Nov 15 '15 at 21:25
  • I was hoping to use v13 so that I can avoid using v4. Turns out v4 is a dependency for v13, so I might as well use the support fragments/view pager. – Vas Mar 30 '16 at 21:15
3

android.app.Fragment class is deprecated since Android P (link), so only android.support.v4.app.Fragment should be used everywhere.

Artyom
  • 1,165
  • 14
  • 22
1

If you're going to target API 11+, you won't need the support library [and your actual apk will be smaller, at least).

If you want to support anything before Android 3.x, you'll need the support library.

Is this what you're asking?

Matt
  • 3,837
  • 26
  • 29
  • 9
    you need to use the support library to use the ViewPager which is what he is asking about – tyczj Jul 09 '13 at 17:01
  • @Matt Thanks for your answer, but I'm looking for the best solution that incorporates `ViewPager` functionality, and an explanation of why that solution is best. – jonstaff Jul 09 '13 at 17:14