When to use support library

前端 未结 4 516
失恋的感觉
失恋的感觉 2021-02-07 10:38

I am confused on the purpose of the Android support library and when it is needed. It is my understanding that the major plus of using the support library is for Android to impl

4条回答
  •  情歌与酒
    2021-02-07 11:09

    Here is your answer—Always!

    The following reasoning is copied straight from Big Nerd Ranch's Android Dev book. Emphasis mine:

    This book uses the support library implementation of fragments over the implementation built into the Android OS, which may seem like an unusual choice. After all, the support library implementation of fragments was initially created so that developers could use fragments on old versions of Android that do not support the API. Today, most developers can exclusively work with versions of Android that do include support for fragments.

    We still prefer support fragments. Why? Support fragments are superior because you can update the version of the support library in your application and ship a new version of your app at any time. New releases of the support library come out multiple times a year. When a new feature is added to the fragment API, that feature is also added to the support library fragment API along with any available bug fixes. To use this new goodness, just update the version of the support library in your application.

    As an example, official support for fragment nesting (hosting a fragment in a fragment) was added in Android 4.2. If you are using the Android OS implementation of fragments and supporting Android 4.0 and newer, you cannot use this API on all devices that your app supports. If you are using the support library, you can update the version of the library in your app and nest fragments until you run out of memory on the device.

    There are no significant downsides to using the support library’s fragments. The implementation of fragments is nearly identical in the support library as it is in the OS. The only real downside is that you have to include the support library in your project and it has a nonzero size. However, it is currently under a megabyte – and you will likely use the support library for some of its other features as well.

    We take a practical approach in this book and in our own application development. The support library is king.

    So... There will always be a support library because you will almost always have to support older devices for a variety of reasons:

    Device owners may not be able to update to the latest version because:

    • Service providers and manufacturers are not bothered about updating a non-flagship type phone - costs money to regression test their bloatware on top of a new version of Android.
    • Some device owners (thankfully not all!) care very little about the Android version on their phone. Totally different situation with the Tinder app though.
    • Device owners may not have the resources to upgrade to a latest/newer device. App developers in developing countries probably face this issue. Google's Platform Versions statistics are not region specific, even though they probably should be!

    Anyway, here is the gist: support libraries have the exact same functionality as OS/framework APIs and they have a compact size—since they have to be included in your APK, they don't increase the size very much. So we have established that there is no downside to using/including them. Now, the upsides are tremendous - look at the Fragment example above.

提交回复
热议问题