I am working on an Android application that is currently targeting devices having a minimum API of 11. However, I want to modify the app to make it compatible with android versions lower than 3.0. The problem is, I have various UI elements that are only provided in the newer versions:
Action bar contains menu items. I read that one solution is using the ActionBarSherlock package. However, the activity already extends ListActivity. Is there any other solution?
Date picker dialog fragment throws an error saying:
"Call requires API level 11 (current min is 8): new android.app.DialogFragment"
This error occurs on the method newFragment.show().
This happens despite having followed the note given on the Android Developer page:
http://developer.android.com/guide/topics/ui/controls/pickers.html
What could be the problem?
However, the activity already extends ListActivity.
Use ActionBarSherlock and change from ListActivity to SherlockListActivity.
What could be the problem?
You are trying to use native API Level 11 fragments instead of the Android Support Library's backport of fragments. IOW, you are using android.app.DialogFragment instead of android.support.v4.app.DialogFragment.
Related
When creating an app in Android Studio and selecting the Activity name I have a button that says "Backwards compatibility (AppCompat)". And below it says "If false, this activity base class will be Activity instead of AppCompatActivity".
What is this backwards compatibility about?
So preview version of Android API level extends just Activity for all level, I remember from froyo and Gingerbread very well, so in these version of API level No Action bar was associated with it, or more or less ActionBar were optional and were mostly implemented with third party libraries. from jellybean, 4.2.X above ActionBar was becoming standard in Android. At this point in time Activity now extends ActionBarActivity instead of the usual 'Activity' and after a while it become 'AppCompat'. So basically the ActionBar support was one of the major difference, as previous API level will not be able to process 'AppCompat', obviously there are some other differnce , but ActionBar is part of the major Differnce. while all API level can still handle the plain usual Activity
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'm coming from iOS where it's easy and you simply use a UIViewController. However, in Android things seem much more complicated, with certain UIComponents for specific API Levels. I'm reading BigNerdRanch for Android (the book is roughly 2 years old) and they suggest I use Activity to host my FragmentActivities. However, I thought Activity was deprecated.
So for API Level 22 (with a minimum support for API Level 15 or 16), what exactly should I use both to host the components, and for the components themselves? Are there uses for all of these, or should I be using one or two almost exclusively?
I thought Activity was deprecated
No.
So for API Level 22 (with a minimum support for API Level 15 or 16), what exactly should I use both to host the components, and for the components themselves? Are there uses for all of these, or should I be using one or two almost exclusively?
Activity is the baseline. Every activity inherits from Activity, directly or indirectly.
FragmentActivity is for use with the backport of fragments found in the support-v4 and support-v13 libraries. The native implementation of fragments was added in API Level 11, which is lower than your proposed minSdkVersion values. The only reason why you would need to consider FragmentActivity specifically is if you want to use nested fragments (a fragment holding another fragment), as that was not supported in native fragments until API Level 17.
AppCompatActivity is from the appcompat-v7 library. Principally, this offers a backport of the action bar. Since the native action bar was added in API Level 11, you do not need AppCompatActivity for that. However, current versions of appcompat-v7 also add a limited backport of the Material Design aesthetic, in terms of the action bar and various widgets. There are pros and cons of using appcompat-v7, well beyond the scope of this specific Stack Overflow answer.
ActionBarActivity is the old name of the base activity from appcompat-v7. For various reasons, they wanted to change the name. Unless some third-party library you are using insists upon an ActionBarActivity, you should prefer AppCompatActivity over ActionBarActivity.
So, given your minSdkVersion in the 15-16 range:
If you want the backported Material Design look, use AppCompatActivity
If not, but you want nested fragments, use FragmentActivity
If not, use Activity
Just adding from comment as note: AppCompatActivity extends FragmentActivity, so anyone who needs to use features of FragmentActivity can use AppCompatActivity.
Activity is the base class of all other activities, I don't think it will be deprecated. The relationship among them is:
Activity <- FragmentActivity <- AppCompatActivity <- ActionBarActivity
'<-' means inheritance here. The reference said ActionBarActivity is deprecated, use AppCompatActivity instead.
So basically, using AppCompatActivity is always the right choice. The differences between them are:
Activity is the basic one.
Based on Activity, FragmentActivity provides the ability to use Fragment.
Based on FragmentActivity, AppCompatActivity provides features to ActionBar.
2019: Use AppCompatActivity
At the time of this writing (check the link to confirm it is still true), the Android Documentation recommends using AppCompatActivity if you are using an App Bar.
This is the rational given:
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.
The general directions for adding a ToolBar are
Add the v7 appcompat support library
Make all your activities extend AppCompatActivity
In the Manifest declare that you want NoActionBar.
Add a ToolBar to each activity's xml layout.
Get the ToolBar in each activity's onCreate.
See the documentation directions for more details. They are quite clear and helpful.
For a minimum API level of 15, you'd want to use AppCompatActivity. So for example, your MainActivity would look like this:
public class MainActivity extends AppCompatActivity {
....
....
}
To use the AppCompatActivity, make sure you have the Google Support Library downloaded (you can check this in your Tools -> Android -> SDK manager). Then just include the gradle dependency in your app's gradle.build file:
compile 'com.android.support:appcompat-v7:22:2.0'
You can use this AppCompat as your main Activity, which can then be used to launch Fragments or other Activities (this depends on what kind of app you're building).
The BigNerdRanch book is a good resource, but yeah, it's outdated. Read it for general information on how Android works, but don't expect the specific classes they use to be up to date.
Activity class is the basic class. (The original) It supports Fragment management (Since API 11). Is not recommended anymore its pure use because its specializations are far better.
ActionBarActivity was in a moment the replacement to the Activity class because it made easy to handle the ActionBar in an app.
AppCompatActivity is the new way to go because the ActionBar is not encouraged anymore and you should use Toolbar instead (that's currently the ActionBar replacement). AppCompatActivity inherits from FragmentActivity so if you need to handle Fragments you can (via the Fragment Manager). AppCompatActivity is for ANY API, not only 16+ (who said that?). You can use it by adding compile 'com.android.support:appcompat-v7:24:2.0' in your Gradle file. I use it in API 10 and it works perfect.
There is a lot of confusion here, especially if you read outdated sources.
The basic one is Activity, which can show Fragments. You can use this combination if you're on Android version > 4.
However, there is also a support library which encompasses the other classes you mentioned: FragmentActivity, ActionBarActivity and AppCompat. Originally they were used to support fragments on Android versions < 4, but actually they're also used to backport functionality from newer versions of Android (material design for example).
The latest one is AppCompat, the other 2 are older. The strategy I use is to always use AppCompat, so that the app will be ready in case of backports from future versions of Android.
Since the name is likely to change in future versions of Android (currently the latest is AppCompatActivity but it will probably change at some point), I believe a good thing to have is a class Activity that extends AppCompatActivity and then all your activities extend from that one. If tomorrow, they change the name to AppCompatActivity2 for instance you will have to change it just in one place.
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?
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.
SO here is my understanding of the purpose of importing fragment library(the v4.jar).
Because the developer wants to use and follow the new Fragment's way of coding, but using this will result compatibility issue on older devices that runs on android 2.xx, therefore, in the project that uses fragment, the developer has to import the v4.jar library so that the older devices know what fragment APIs and are able to call them in the app.
If this is correct, I suppose after importing the library, the android:minSdkVersion should be able to set to older version such as 8 (93% of the market), not l0, because the project had already import the library.
But When I am using the method from the fragment, for example, the instantiate method, I got this error:
Call requires API level 11 (current min is 8): android.app.Fragment#instantiate
So apparent the sdk wants me to switch my android:minSdkVersion to 11, but then what is the purpose of importing the library? And by doing so older device that running on android 2.xx will not be able to use this app.
Can someone explain this issue ?
It sounds like you're importing the wrong Fragment library.
This one requires API level 11 (Android 3.0):
http://developer.android.com/reference/android/app/Fragment.html
This one, as available in the support library, requires API level 4 (Android 1.6):
http://developer.android.com/reference/android/support/v4/app/Fragment.html
Also, you should be aware that you need to use the FragmentManager available in the support library.
There are no drawbacks to using the support Fragment APIs on Android 3.0+.