Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does v4 support library use new classes when available?

I know that the v4 support library can be used to implement in old android versions things that have been introduced in more recent versions, such as Fragments.. If we implement an app that uses the v4 support library to display Dialog Fragments, for example, does it use the latest code (i.e. the original and newer fragments) when it runs on ICS or does it still use the support code for every android version?

Is there a way to use in the same app both the android.app.Fragment class and the android.support.v4.app.Fragment class, distinguishing at runtime if we are running on a Fragment-enabled release of android or do we need to use only the support classes when we import the v4 support library?

Maybe I'm wrong, but it does not seem to me a good idea not to use the latest code when running on recent platforms..

Sticking to the Dialog Fragment case, what do you think it is better:

1) use the v4 support library, i.e. use compatibility code on all android versions

2) use the new android.app.DialogFragment when running on API level 11 or above and use the deprecated showDialog and onCreateDialog methods of the Activity class when running on API level < 11

As I've already said, the best one IMHO would be the following, but (correct me if I'm wrong) it is not a possible solution:

3) use the new android.app.DialogFragment class when running on API level 11 or above and use the android.support.v4.app.DialogFragment class when running on API level < 11

I'm sorry if I made a bit of confusion, I hope the question is quite clear..

like image 408
Gianni Costanzi Avatar asked Jun 29 '12 14:06

Gianni Costanzi


3 Answers

When you use the android support libary in your project, even if the device has a compatible API level of Android so that it does not need to use the compatibility library, it will still use the methods from compatibillity library.

EDIT

Browsing the Android support library v4 code it states (in comments in android.support.v4.app.Fragment class):

Static library support version of the framework's android.app.Fragment. Used to write apps that run on platforms prior to Android 3.0. When running on Android 3.0 or above, this implementation is still used; it does not try to switch to the framework's implementation. See the framework SDK documentation for a class overview.

Link here.

like image 106
Angelo Avatar answered Nov 13 '22 18:11

Angelo


If we implement an app that uses the v4 support library to display Dialog Fragments, for example, does it use the latest code (i.e. the original and newer fragments) when it runs on ICS or does it still use the support code for every android version?

For fragments in particular, it always uses its own backport. In other cases, it may pass through to a native implementation where available.

Is there a way to use in the same app both the android.app.Fragment class and the android.support.v4.app.Fragment class, distinguishing at runtime if we are running on a Fragment-enabled release of android or do we need to use only the support classes when we import the v4 support library?

I would stick to the support classes. You would be adding a ton of additional work for no real benefit by going your alternative approach.

Maybe I'm wrong, but it does not seem to me a good idea not to use the latest code when running on recent platforms.

You are welcome to your opinion. For larger subsystems like fragments, the benefits would be outweighed by the costs IMHO.

Sticking to the Dialog Fragment case, what do you think it is better:

The best option is to not use DialogFragment at all, instead finding a way to accomplish what you want without using dialogs.

Of the three options you presented, use option #1 for the time being. Once you feel that you can drop support for Android 2.x, you can switch to using native fragments throughout your app.

like image 21
CommonsWare Avatar answered Nov 13 '22 16:11

CommonsWare


You can see the API level programmatically with this :

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.FROYO){
// Do something for froyo and above versions
} else{
// do something for phones running an SDK before froyo
}

Then, you could duplicate your code to use the class you want by writing the entire name of the class in your code, ie android.app.DialogFragment or android.support.v4.app.Fragment and not just DialogFragment.

like image 20
Stephane Mathis Avatar answered Nov 13 '22 18:11

Stephane Mathis