Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Support libraries when developing for Android 4.1 and higher

The current minSdkVersion for my app is 16, targetSdkVersion,compileSdkVersion are 21.

When I first started the app, minSdkVersion was 10, and the book I was learning from used the v4 Support library.

I've gone through my app and I found that these are the imports I have been using from the v4 support library:

ActionBarDrawerToggle
DialogFragment
DrawerLayout
Fragment
FragmentActivity
FragmentManager
FragmentStatePagerAdapter
ListFragment
NavUtils
ViewPager

Since I have raised the minSdkVersion, I don't think I need to use the support versions of most of these anymore. I believe I still have to use ViewPager and DrawerLayout, though.

The developer pages say:

Caution: When using classes from the Support Library, be certain you import the class from the appropriate package.

For example, when applying the ActionBar class:

android.support.v7.app.ActionBar when using the Support Library.

android.app.ActionBar when developing only for API level 11 or higher.

So, my question is:

1) May I simply use an import similar to android.app.Fragment for all of the above, since my API level is minimum 16? (Except ViewPager and DrawerLayout)

2) For ViewPager and DrawerLayout, is there a difference between using v4 or v7 or another support library version?

Any advice is appreciated. Thanks!

like image 368
OnThisDayFiftyYearsAgo Avatar asked Jun 12 '26 04:06

OnThisDayFiftyYearsAgo


1 Answers

First, with Fragment and all its related paraphernalia (FragmentManager, FragmentTransaction, &c) the most important thing is to use the imports consistently (that is, don't mix and match). That, I think, is the point the documentation wants to make.

That said...

You could swap out usages of android.support.v4.app.Fragment with android.app.Fragment if you wanted to use the native versions of these classes instead of the support ones, but be careful: the support library also adds some methods and callbacks that were not present in API level 16 (one important example being getChildFragmentManager(). So, basically, it comes down to whether all the features of Fragments that you're using are available for the native Fragments in all the API levels you will support.

Also, using the support library Fragments means that they should behave exactly the same in all Android versions, while they might be differences in the native implementations.

In our particular case we decided to keep using the support library, but YMMV.

As for the second question, I don't think there is a v7-specific ViewPager (at least, I think so). The v7 libraries are add-ons (appcompat, gridlayout, &c).

like image 84
matiash Avatar answered Jun 13 '26 18:06

matiash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!