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?
This question is related to
android
android-fragments
android-activity
android-actionbaractivity
appcompatactivity
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
AppCompatActivity
NoActionBar
.ToolBar
to each activity's xml layout.ToolBar
in each activity's onCreate
.See the documentation directions for more details. They are quite clear and helpful.
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.
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.
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.Activity
, FragmentActivity
provides the ability to use Fragment
.FragmentActivity
, AppCompatActivity
provides features to ActionBar
.If you talk about Activity
, AppcompactActivity
, ActionBarActivity
etc etc..
We need to talk about Base classes which they are extending, First we have to understand the hierarchy of super classes.
All the things are started from Context which is super class for all these classes.
Context is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc
Context
is followed by or extended by ContextWrapper
The ContextWrapper is a class which extend Context class that simply delegates all of its calls to another Context. Can be subclassed to modify behavior without changing the original Context.
Now we Reach to Activity
The Activity is a class which extends ContextThemeWrapper that is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you
Below Classes are restricted to extend but they are extended by their descender internally and provide support for specific Api
The SupportActivity is a class which extends Activity that is a Base class for composing together compatibility functionality
The BaseFragmentActivityApi14 is a class which extends SupportActivity that is a Base class It is restricted class but it is extend by BaseFragmentActivityApi16 to support the functionality of V14
The BaseFragmentActivityApi16 is a class which extends BaseFragmentActivityApi14 that is a Base class for {@code FragmentActivity} to be able to use v16 APIs. But it is also restricted class but it is extend by FragmentActivity to support the functionality of V16.
now FragmentActivty
The FragmentActivity is a class which extends BaseFragmentActivityApi16 and that wants to use the support-based Fragment and Loader APIs.
When using this class as opposed to new platform's built-in fragment and loader support, you must use the getSupportFragmentManager()
and getSupportLoaderManager()
methods respectively to access those features.
ActionBarActivity is part of the Support Library. Support libraries are used to deliver newer features on older platforms. For example the ActionBar was introduced in API 11 and is part of the Activity by default (depending on the theme actually). In contrast there is no ActionBar on the older platforms. So the support library adds a child class of Activity (ActionBarActivity) that provides the ActionBar's functionality and ui
In 2015 ActionBarActivity is deprecated in revision 22.1.0 of the Support Library. AppCompatActivity should be used instead.
The AppcompactActivity is a class which extends FragmentActivity that is Base class for activities that use the support library action bar features.
You can add an ActionBar to your activity when running on API level 7 or higher by extending this class for your activity and setting the activity theme to Theme.AppCompat
or a similar theme
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.
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.
Source: Stackoverflow.com