Adding support for Fragments in API 10

1.7k views Asked by At

I've been building an app that untill recently had minSdkVersion="14", but i want to change that and add compatability for api 10. The first problem i had was with styles but i sort-off fixed it by using Theme.AppCompat. Now i have problems in my code, the first one is that i'm using a lot of fragments.

The first fragments that appear in my app are:

FirstRunNotice firstRunNotice = new FirstRunNotice();
firstRunNotice.show(getFragmentManager(), "WhatDoesThisStringEvenDo?");
//.show method gives an error

This is just an inner class within my activity that extends DialogFragment. How do i make this work on API 10? If i change it to android.support.v4.app.DialogFragment it seems to take the errors away (using Android Studio) but it's weird because if i use it on one inner class (i have the DialogFragments) it takes the error away on both. Why is that?

Also, if i were to change all Fragment extended classes to android.support.v4.app.Fragment, .DialogFragment, .ListFragment... What would that do in case i run my app on a higher API, let's say 19? Would the app use the compatability library or would it know to use the class from that API? And is there a difference?

1

There are 1 answers

8
A--C On BEST ANSWER

Since now you want to support Gingerbread and lower, you have to use the android.support.v4.app.* Fragment classes for your app to compile and run. The call to getFragmentManager() should also be replaced with getSupportFragmentManager()

It is also important to note that calling getSupportFragmentManager() is only part of FragmentActivity and ActionBarActivity (the latter is an extention of the former. It is a part of Google's ActionBarCompat library).

This is because since the support Fragments are an addition to the Android system, there needs to be a way to implement them without relying too much on the Android internals (since Gingerbread and lower have no notion of a Fragment).

but it's weird because if i use it on one inner class (i have the DialogFragments) it takes the error away on both. Why is that?

It's likely you are encountering multiple Android Lint errors

What would that do in case i run my app on a higher API

Nothing, the support library works with new API versions as well. Of course, if you had previous code that required API 11+, then you need to figure out a way to backport that as well.

Would the app use the compatability library or would it know to use the class from that API?

The support Fragment documentation says:

When running on Android 3.0 or above, this implementation is still used; it does not try to switch to the framework's implementation.

Support Fragments will always be used. Usually, this isn't an issue.