2

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?

A--C
  • 36,351
  • 10
  • 106
  • 92
Squeazer
  • 1,234
  • 1
  • 14
  • 33

1 Answers1

3

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.

A--C
  • 36,351
  • 10
  • 106
  • 92
  • Thanks for the explenation! The only thing i cant get to work is `getFragmentManager()`, if i just change it to `getSupportFragmentManager()` the method doesnt resolve... – Squeazer Dec 08 '13 at 00:40
  • @Squeazer Are you now extending `FragmentActivity` or `ActionBarActivity`? I assumed so because you are using `Theme.AppCompat`. – A--C Dec 08 '13 at 00:42
  • The first activity extends `Activity`. It's basically a splash screen for the application that loads some xml files from the web. The `gerFragmentManager()` is called from `onCreate()` and from an inner `AsyncTask` class. – Squeazer Dec 08 '13 at 00:45
  • You do need to extend one of the classes I mentioned for `getSupportFragmentManager()` to resolve (I edited the answer to include this info) – A--C Dec 08 '13 at 00:50
  • This worked perfectly thanks! Just one more theng, if you could point me in the right direction... I also have a widget with a list, it uses `RemoteViewsService`, `RemoteViewsFactory` etc.... It's API 11+, how can i add compatability for that? The widget is nothing fancy, just a list with some items in it. – Squeazer Dec 08 '13 at 01:12
  • Unfortunately you cannot. You could use LinearLayout to sort of simulate a list (no scrolling either on API 10 and lower, so that's another restriction). – A--C Dec 08 '13 at 01:37
  • Oh that's too bad. Is there a way to disable the widget in lower APIs? So it wouldnt show up in the widget list? – Squeazer Dec 08 '13 at 01:42
  • 1
    Nevermind, found the answer here: [link](http://stackoverflow.com/questions/7064035/is-it-possible-to-show-widget-only-for-a-certain-android-version) Thanks for the help! – Squeazer Dec 08 '13 at 01:47