Difference between extending LifecycleActivity,Activity,ActionbarActivity & AppCompactActivity?
- extending
ActionBarActivity
gives you theActionBar
s functionality on every API level >= 7 - by extending
Activity
you can avoid adding additional projects/libraries to your project but you'll lack theActionBar
on api levels below 11
edit: More details:
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
edit2: Update April 2015 - it looks like the ActionBarActivity
is deprecated in revision 22.1.0
of the Support Library. AppCompatActivity
should be used instead.
edit3: Update Aug 2017 - LifecycleActivity is a LifecycleOwner but:
"Since the Architecture Components are in alpha stage, Fragment and
AppCompatActivity classes cannot implement it (because we cannot add a
dependency from a stable component to an unstable API). Until
Lifecycle is stable, LifecycleActivity and LifecycleFragment classes
are provided for convenience. After the Lifecycles project is
released, support library fragments and activities will implement the
LifecycleOwner interface; LifecycleActivity and LifecycleFragment will
be deprecated at that time."
(copied from the Architecture Components guideline)
Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which?
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
.
Difference between Java class extends AppcompatActivity vs Activity vs ActionBar in Android?
- extending
ActionBarActivity
gives you theActionBar
s functionality on every API level >= 7 - by extending
Activity
you can avoid adding additional projects/libraries to your project but you'll lack theActionBar
on api levels below 11
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
- The new deprecated version of
ActionBarActivity
(the one extendingAppCompatActivity
class) is a safe to use backward compatibility class. Its deprecation is just a hint for you to use newAppCompatActivity
directly instead.AppCompatActivity
is a new, more generic implementation which usesAppCompatDelegate
class internally.
For instance, you inherit an activity from an external library, which, in turn, does not inherit from AppCompatActivity
but you want this activity to have tinted materials widgets (views). To make it happen you need to create an instance of AppCompatDelegate
inside your activity, override
methods of that activity like addContentView()
, setContentView()
etc. (see AppCompatDelegate
javadoc for the full list of methods), and inside those overridden methods forward the calls to inner AppCompatDelegate
instance. AppCompatDelegate
will do the rest and your "old-fashion" activity will be "materialized".
Source: this and this.
Difference between ActionBarActivity and Fragment Activity
FragmentActivity
is the base class for support based fragments. So you will be using Fragment
from support library below api level 11 in which case your Activity
needs to extend FragmentActivity
.
↳ android.support.v4.app.FragmentActivity
↳ android.support.v7.app.ActionBarActivity
You will use ActionBarActivity
when you need actionbar below API level 11 by using AppCompat library. In this case your Activity extends ActionBarActivity
.
As you see ActionBarActivity
extends FragmentActivity
Why my first android MainActivity class extends ActionBarActivity instead of Activity?
It depends on the type of Activity
you choose when creating a new project via the wizard (File>New>Android Application Project
). If you choose Empty Activity
at the Create Activity
page, you should get a starting point similar to the tutorials.
Difference between Activity and FragmentActivity
A FragmentActivity
is a subclass of Activity
that was built for the Android Support Package.
The FragmentActivity
class adds a couple new methods to ensure compatibility with older versions of Android, but other than that, there really isn't much of a difference between the two. Just make sure you change all calls to getLoaderManager()
and getFragmentManager()
to getSupportLoaderManager()
and getSupportFragmentManager()
respectively.
Android Fragments : support library crashes on extending Activity class
You have to choose whether you use classes from the support library or not. If you do, you have to use classes that are compatible with each other. FragmentActivity
and ActionBarActivity
are part of the support library, hence they support android.support.v4.app.Fragment
. Activity
is not from the support lib, so it supports android.app.Fragment
.
Basically, Activity
and ActionBarActivity
do the same things. There are minor differences between the 2, the main one being the method getFragmentManager()
in Activity
being replaced by getSupportFragmentManager()
in the support library. Other methods that differ are usually prefixed with 'support' in ActionBarActivity
.
Related Topics
Download Multiple Files with a Progress Bar in Listview Android
Android Alert Dialog with One, Two, and Three Buttons
How to Implement Export SQLite to Excel/CSV File in Android
How to Tell If a Closed Path Contains a Given Point
Extended Surfaceview's Ondraw() Method Never Called
How to Make an Intent with Multiple Actions
Mylib.So Has Text Relocations. This Is Wasting Memory and Is a Security Risk. Please Fix
Using Android to Submit to a Google Spreadsheet Form
Use Recyclerview Inside Scrollview with Flexible Recycler Item Height
Android Saving Bitmap to Sd Card
Change the Right Margin of a View Programmatically
Encoding H.264 from Camera with Android Mediacodec
Broadcast Receiver Won't Receive Camera Event
Android and Setting Alpha for (Image) View Alpha
How to Implement Uncaughtexception Android
Android Open Camera from Button
Setrotation(90) to Take Picture in Portrait Mode Does Not Work on Samsung Devices