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?
2018: 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:
The general directions for adding a ToolBar are
AppCompatActivity
NoActionBar
.ToolBar
to each activity's xml layout.ToolBar
in each activity'sonCreate
.See the documentation directions for more details. They are quite clear and helpful.
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 classActivity
that extendsAppCompatActivity
and then all your activities extend from that one. If tomorrow, they change the name toAppCompatActivity2
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, useAppCompatActivity
instead.So basically, using
AppCompatActivity
is always the right choise. The differences between them:Activity
is the basic one.Activity
,FragmentActivity
provides the ability to useFragment
.FragmentActivity
,AppCompatActivity
provides features toActionBar
.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
andAppCompat
. 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 useAppCompat
, so that the app will be ready in case of backports from future versions of Android.For a minimum API level of 15, you'd want to use
AppCompatActivity
. So for example, yourMainActivity
would look like this: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:You can use this
AppCompat
as your mainActivity
, 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.
AppCompatActivity extends FragmentActivity extends BaseFragmentActivityApi16 extends BaseFragmentActivityApi14 extends SupportActivity extends Activity
So Activity is faster than all & AppCompatActivity is the best of all.