ViewPager without Android.Support.v4


#1

Leaving a breadcrumb for people who follow this path… I’m focusing on tablet apps and newer devices, so from the beginning of CriminalIntent, I’ve set my minimum API level to 14, and did not include Android.Support.v4. That worked fine, until I needed a ViewPager for this chapter. There is no ViewPager in the main Android libraries! Introducing the v4 support library at this point caused cascading incompatibilities, so I started looking for ways to implement the functionality without ViewPager. This was not a good idea for a newbie Android dev.

However, there is a newer support library, Android.Support.v13. You still need the v4 library for ViewPager, but v13 makes it work smoothly with the main Android libraries for activities, fragments, adapters, etc. So this appears to be the answer.

BTW, excellent book so far! I really appreciate the focus on immersing in code from page 1. Because I’m interested in cross-platform mobile app development, I’m using Xamarin.Android and C# rather than Eclipse and Java, but so far, so good.


#2

Great catch! I’ll note that in my copy.

This is part of why we stick with the v4 support library, by the way. The support library gets a lot of love these days. So even where it is possible, we would elect not to use android.app.Fragment and friends. Plus, many third party libraries depend on the support library.

(And thank you!)


#3

I went down the same path as cyloncat and tried to use the ‘android.app.Fragment’ set of classes. Reverted everything back to the v4 support libraries in the end since I couldn’t find an alternate solution. I’m wondering how the v4 libs compare with v13. Any reason not to use v13? I guess you still need v4 as well to use FragmentStatePagerAdapter.

Is it your opinion that we should just stick with the v4 support library when using Fragments? If we are only developing for say, 4.2 Jellybean, do we lose anything by doing so?

Also, should the AUF principle be changed to AUFv4 :slight_smile:

Btw, I’m really enjoying the book. Thanks!


#4

As you might expect, compatibility is the big thing you miss out on. That’s compatibility not just with older versions of Android, but with third party libraries that will (typically) use support library fragments instead.

There is one major advantage that I know of to using Android 4.2 fragments, and that is that in nested fragments, onActivityResult is not fired correctly. We don’t cover nested fragments in the book, of course, but if you use them you probably want to be aware of this.

(I picked this up from this great blog post, btw - http://delyan.me/04-28-2013/android-s-matryoshka-problem/. If you’re interested in using nested fragments, read it! Now! :slight_smile: It tells you about some drawbacks that could really bite you if you don’t watch out.)


#5

(I do recommend sticking with v4 fragments, by the way. I haven’t played around with v13 yet, and I probably won’t for a while.)


#6

Cyloncat, could you provide a little more detail about how you made ViewPager work with a standard Activity, Fragment, etc? I tried using support library v4 for ViewPager and v13 support library for FragmentStatePagerAdapter with a standard Activity and Fragment. The resulting program crashed rather spectacularly…


#7

Hi guys. Love the book. I too took the rash decision in chapter 7 to develop just for API level 11 and up (based on the current usage numbers published on the Android developer site), and now in chapter 11 I’ve come unstuck with the ViewPager implementation! It might be worth putting a warning in the ‘For the More Curious’ section in chapter 7 that later sections rely on the use of the support library. Also worth mentioning the compatibility issues with third party libraries if the support library is not used.


#8

Thanks for pointing that out. There is some slightly dated discussion on stackoverflow as well. http://stackoverflow.com/questions/17553374/android-app-fragments-vs-android-support-v4-app-using-viewpager


#9

This is exactly the same problem I’m having. I tried including:

import android.support.v13.app.FragmentStatePagerAdapter;

but I get it cannot be resolved.

Early on in the book it says that you dont have to use the support lib if your developing for the newer APIs. Then in chapter 11 it turns out you do have to use the support.v4 lib.


#10

I ran into this as well. I’ve been going through the book using Android Studio (recently out of preview and formally in beta now).

To resolve this I did:

  1. File -> Project Structure -> Dependencies -> Library Dependency -> com.android.support-v4
  2. Change the dependency in build.gradle to match your version (this is necessary right now probably because of the L release preview being available but not complete)

This will allow you to call android.support.v4 w/o throwing a bunch more errors in the rest of your project

Explainers here:

  1. stackoverflow.com/questions/2024 … udio-0-3-6
  2. stackoverflow.com/questions/2443 … version-14

#11

If anyone encounters this same issue when trying to do this lesson with a project built against a newer SDK version, the Nerd Ranch download code has projects done for each chapter. No need to redo the entire project.


#12

Having also taken the route of API 11+ development, the detailing of the alternate android.app classes in the ‘For the More Curious’ section at the end of chapter 7 emboldened my choice to go API 11+, because it gave me the impression that the authors would be giving us similar information in subsequent chapters. I wish they would have said that we have to use android.support.v4, because they were going to use features of it for which they would not be supplying a workaround in chapter 11. :frowning:


#13

The v13 solution seems to work for me:

(1) File -> Project Structure -> Dependencies -> Library Dependency -> com.android.support-v13
(2) import android.support.v13.app.FragmentStatePagerAdapter;

Then you can simply use the standard android library without v4.