When I created a new app, I got this
class NewApplicationActivity extends ActionBarActivity{... }
So I have read that, if you want to use a new feature from a newer API level, you better make it such that there is an alternative option for the App to combat any situation for the minSDKVersion that you plan to support.
Assuming this is true, lets say I decide to build my App against targetSDKVersion = 21(which I did in the above example), I would get the base Activity class as ActionBarActivity. Now this is from support library and works for older version(down to Android 2.1 I guess..).
The thing is, I am stuck with these alternative set of support libraries rather than platform libraries. Am I gonna miss something significant if this happens? Will I get a chance to incorporate the platform specific code from API 21 into my App ever? The majority of Apps are made with compatibility in mind. How do you people handle this?
Support Libraries such as AppCompat (which is what ActionBarActivity and its replacement AppCompatActivity are part of) are designed to reflect the latest platform changes and backport as much as possible. Thereby by using AppCompat, you are already using a large number of API 21 features (such as material theme).
Of course, there is nothing stopping you including any API from any level in your application: just make sure that they are guarded by the appropriate API level checks: this is exactly what many of the Compat classes in Support v4 such as NotificationCompat and ViewCompat do for you.
Related
I am getting started with Android. I am quite confused with what support library actually does . I am following a book and in the book it is mentioned that :
If an app is installed on a minimum SDK system, and our Java code
contains any calls to classes that are not present in minimum SDK ,
our app will crash
I read an article about support libraries here:
Tutorials Point
If Support libraries provide backward compatibility, doesn't it mean when they should prevent app crash?(may be by wrapping up those newer classes and making them backward compatible). What does support library actually do? Please explain.
When should I use the Android Support Library?
As new versions of Android are released, some style and behaviors may change. You should use one of the support libraries when you need specific framework features that are newer than the minSdkVersion of your app or that are not available in the standard framework.
What does support library actually do?
There are many devices still running in Jelly bean and below versions. So you need to give the same importance for old and new devices While making an app. Otherwise, the App in old devices looks dull when compared to new ones. That may affect the app’s market. To avoid this trouble, Android introduced the Support library. These are set of code libraries provides backward compatibility
Example:
Fragment API was introduced in API 11 HONEYCOMB .android.app.Fragment is the Fragment class introduced in API 11 and android.support.v4.app.Fragment is the Fragment class in the android support library,
If you want to make your app use fragments, and want to target devices before API 11, you must use android.support.v4.app.Fragment. However, if you want only targeting devices >=API 11 ,you can use android.app.Fragment.Therefore, android.support.v4.app.FragmentManager is used to deliver newer features to older platforms.
For more info about android support library:doc
1.Suppose you want to create an app which runs on platform comes after marshmallow.
then minimum sdk of your app will be marshmallow.
2.While creating your app you call a method which is present in oreo or later version then you app will have chances to crash on marshmallow and nougat.
Android dashboards show that only half of devices have Android 5.0 and above, but numbers look different for our customer data set - it's over 93% of our users. So we've decided to abandon support for devices with Android version lower than 5.0 and change minSdkVersion from 15 to 21.
This upgrade require from us to review all of obsolete functionalities and clean some hacky workarounds which we applied to support older versions. One of main functionality which we can apply now is to replace android.support.v4.app.Fragment with android.app.Fragment. It sounds like good idea, especially when we know that fragment API had been reviewed and improved.
Just to make sure that I’ll take right path and android.app.fragment won’t surprise me I would like to paraphrase question from this stackoverflow thread - Are you using fragments from the support library, even though they are already available, when developing for Android 5? Are there any bugs in fragment API which occur on Android 5.0 and above and were fixed in support library?
Update
After Android-KTX release, Jake Wharton made the following statement in one of PRs:
Thanks for taking the time to make the PR (with tests!), but we would like to encourage that developers only use the support library fragments. The next version of Android will deprecate the version of fragments that are part of the platform. Thus, we aren't going to add any extensions to support them in this project.
So using fragment from support library is the right thing to do.
I would suggest using the support library fragments anyway, for a few reasons:
Future features may be added and backported, and then you'd need to rewrite to use them
Consistency. The support library works to make sure support fragments always work the same. Whereas the native fragments may have subtle differences between versions.
The support library can contain bug fixes to the platform and can be updated much more frequently.
Edit: As of 2018, the OS level Fragment is deprecated. Google's path forward technically is to do less in the OS library and more in add ons. So for Fragment this is absolutely now support library, and likely to go this way for more items.
We started a fresh Android app few months back and we were wondering the same thing.
IMO, you should always try to use android.app.Fragment if it is possible. This way, you are certain that Android developers optimize, update and support this API and they will give you a quality product.
Right now, we are using android.support.v4.view.ViewPager and other similar components that provide different functionality in Support library than in the normal API.
I am quite new to Android and Android Studio. I have created a new project and I have specified the minSdkVersion as 21 and target to 23.
After AS is done with creating the activity I see that MainActivity is extending AppCompatActivity by default.
I read about AppCompatActivity here
It says that it is the Base class for activities that use the support library action bar features.
Now my question is:
Since my app's minSdkVersion is 21 why do I need my activity to extend AppCompatActivity?
Why does AS make my activity extend AppCompatActivity by default?
Is it necessary for my activity to extend AppCompatActivity or just extending Activity is enough considering my minSdkVerion?
What would I miss if my actvity don't extend AppCompatActivity?
Any explanation would be very helpful. Thanks
I think you should extend AppCompatActivity if you are using action bar
Read more here
Beginning with Android 3.0 (API level 11), all activities that use the default theme have an ActionBar as an app bar. However, app bar features have gradually been added to the native ActionBar over various Android releases. As a result, the native ActionBar behaves differently depending on what version of the Android system a device may be using. By contrast, the most recent features are added to the support library's version of Toolbar, and they are available on any device that can use the support library.
For this reason, you should use the support library's Toolbar class to implement your activities' app bars. Using the support library's toolbar helps ensure that your app will have consistent behavior across the widest range of devices. For example, the Toolbar widget provides a material design experience on devices running Android 2.1 (API level 7) or later, but the native action bar doesn't support material design unless the device is running Android 5.0 (API level 21) or later.
AppCompatActivity gives you the additional functionality of ActionBar after api level 7.
Activity helps you avoid extra libraries but doesnt provide actiobBar feature.
Prior to revision 22.1.0 the Action bar was provided by extending ActionBarActivity which is deprecated now
Well, as you said, indeed it is. We don't have to extends AppCompatActivity. But a lot of open source libs using AppCompatActivity to grab the feature to lower API level projects, and the third part libs are going to ask your MainActivity to extends AppCompatActivity. So, either you can implement your own lib or extends AppCompatActivity.
AppCompatActivity is a part of support library, so you also have the benefit of using support library. In short, your activity which extended AppCompatActivity will have the capability of the Activity in the recent API (but as long as the implementation of the capability is builded in the support library).
You can read the following from documentation:
Using Support versus Framework APIs
Support Libraries provide classes and methods that closely resemble APIs in the Android Framework. Upon discovering this, you may wonder if you should use the framework version of the API or the support library equivalent. Here are the guidelines for when you should use support library classes in place of Framework APIs:
Compatibility for a Specific Feature - If you want to support a recent platform feature on devices that a running earlier versions of the platform, use the equivalent classes and methods from the support library.
Compatibility for Related Library Features - More sophisticated support library classes may depend on one or more additional support library classes, so you should use support library classes for those dependencies. For example, the ViewPager support class should be used with FragmentPagerAdapter or the FragmentStatePagerAdapter support classes.
General Device Compatibility - If you do not have a specific platform feature you intend to use with your app in a backward compatible way, it is still a good idea to use support library classes in your app. For example, you may want to use ActivityCompat in place of the framework Activity class, so you can take advantage of newer features later on, such as incorporating the new permissions model introduced in Android 6.0 (API level 23).
I understand that it is considered good practice to program for Android using the AppCompat support libraries for backwards compatibility. However, for this specific project, the minimum API level I am to support is Android 5.0 (level 22).
However all information I can find for new Lollipop features always points towards the AppCompat support libraries. Must I do this, or are there vanilla equivalents that do not require AppCompat themes, classes or attention to backwards compatibility (ie. a plain CoordinatorLayout in layout XML instead of android.support.design.widget.CoordinatorLayout)?
First, Andorid 5.0 is API 21. If you set the min SDK to 21, most features like material design theme comes by default, you don't need any libraries. But I think the CoordinatorLayout is only in the com.android.support:design library, you have to include the library to use it.
You will need to use various support libs for certain functionality.
The CoordinatorLayout exists in the design library, and not in the OS framework.
This can actually be quite useful. Use of the design library means you can use these components independently from the OS version of the user's device. Meaning you can get updated functionality without the user updating their OS.
And won't experience breaking API differences depending on what OS the user runs.
Reading through official documentation of Android made me little bit confused about this 2 libraries. When should I use one and when the other one?
As far as I'm understanding, it's the best to use Android Support library depending to the number of devices that will be able to run it and the look will stay always the same. No matter what might get in the future of the android, Support library will always be supported on any future Android API. But why is then DialogFragment for android.app? It is logic to me that android.app.DialogFragment has some benefits which that from support's doesn't because anyways it would be useless to have it, since it's not supported on so many devices.
Can you help me which I should prefer to use it and if my sayings were right?
If your app needs to be compatible with Android 2.x you should use the DialogFragment from the Support Library. Notice that adding the Support Library to your project makes your app bigger because the JAR of the Support Library will be included in your APK.
If you only support Android 3.x or higher you can stick with the DialogFragment built-in into the OS.
Both versions of the API offer (roughly) the same functionality.