Fragments in Android 2.2.1, 2.3, 2.0. Is This Possible

Fragments in Android 2.2.1, 2.3, 2.0. Is this possible?

You have to use the compatibility libraries provided by Google. Here's how you use Fragments on devices < 3.0

  • Open Eclipse
  • Window->Android SDK and AVD
  • Available Packages->Android Support package (install this)

Once installed, right click the Android project you want to add Fragment support for.

  • Build Path->Configure Build Path
  • Libraries tab
  • Add External JARs
  • Add the android-support-v4.jar (should be in the android downloads folder under extras/android/support/v4

Now you application supports Fragments. There are some key differences to using the compatibility package over using SDK 3.0+. For instance

  1. The activity classes that use fragments must extend FragmentActivity NOT Activity.
  2. instead of getFragmentManager() you have to use getSupportFragmentManager


android fragments make it work on android 2.2

Changed and to extend FragmentActivity instead of just Activity.

Using the v4 Library APIs

How to provide support for Fragments and ActionBar for App which supports Android 2.2+ devices?

All your code is look good , just keep in mind that you have two xml file activity_main.xml one will be in layout folder and second will be in layout-port folder.

For complete working demo of Vogella check out this link project.

Error inflating class android 2.2 with drawer navigation

In case anybody has the same problem the issue was the android:background


crushed the whole app. So I removed it in the pre-Honeycomb versions

Choice of design between using Inheritance or Fragments

That depends on your situation. Lets say you have an Activity A that does some stuff and an Activity B that does exactly the same stuff like A but a little bit more (like adding a few buttons).

In that case you can let Activity B extend your Activity A.
B just adds what's missing.

If Activity A and B have a lot in common, but if there are also slight differences, to be precise: if Activity A contains code that is not needed by B you should create one abstract base class C for both activities A and B which contains only the common sub-set of code. Let A and B extend C and add to A and B their individual extra-code.

As for using Fragments or not: I'd go for it. In the long run you'll have less troubles with supporting tablets / multiple screen sizes and you can easily re-use them in different contexts.

Let's assume your boss / client suddenly wants that fancy ViewPager feature we know from Google Play (swiping screens left and right for switching categories). If you were using Fragments in the first place, this change can be done very easily and quickly.
If you were using Activities instead, you need to re-arrange a great deal of code.

Related Topics

Leave a reply