What's the enhancement of AppCompatActivity over ActionBarActivity?
As Chris wrote, new deprecated version of ActionBarActivity
(the one extending AppCompatActivity
class) is a safe to use backward compatibility class. Its deprecation is just a hint for you asking to use new AppCompatActivity
directly instead. AppCompatActivity
is a new, more generic implementation which uses AppCompatDelegate
class internally.
If you start a new development, then you should rather use new AppCompatActivity
class right away. If you have a chance to update your app, then replace deprecated ActionBarActivity
by the new activity as well. Otherwise you can stay with deprecated activity and there will be no difference in behavior at all.
Regarding AppCompatDelegate
, it allows you to have new tinted widgets in an activity, which is neither AppCompatActivity
nor ActionBarActivity
.
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 of those overridden methods forward the calls to the inner AppCompatDelegate
instance. AppCompatDelegate
will do the rest and your "old-fashion" activity will be "materialized".
The impact of changing ActionBarActivity to AppCompatActivity
Copied from sergej shafarenka's answer
ActionBarActivity
(the one extending AppCompatActivity
class) is a safe to use backward compatibility class. Its deprecation is just a hint for you asking to use new AppCompatActivity
directly instead. AppCompatActivity
is a new, more generic implementation which uses AppCompatDelegate
class internally.
If you start a new development, then you should rather use new AppCompatActivity
class right away. If you have a chance to update your app, then replace deprecated ActionBarActivity
by the new activity as well. Otherwise you can stay with deprecated activity and there will be no difference in behavior at all.
Regarding AppCompatDelegate
, it allows you to have new tinted widgets in an activity, which is neither AppCompatActivity
nor ActionBarActivity
.
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".
Should I replace ActionBarActivity with AppCompatActivity?
Yes. You should use AppCompatActivity
.
The AppCompatActivity
is a very new class and the training section on the android website is outdated.
Everything that you would do with ActionBarActivity
will work mostly the same on AppCompatActivity
.
For differences please read the post about it http://android-developers.blogspot.de/2015/04/android-support-library-221.html
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
.
diffrence between Appcompact activity and actionbar activity
ActionBarActivity
is now deprecated. As of version 22.1 of the support library you should use AppCompatActivity
.
Chris Banes (one of the authors of the AppCompat library) covered the refactor in detail here.
Why was ActionBarActivity deprecated
ActionBar is deprecated ever since Toolbar
was introduced. Toolbar
can be seen as a 'superset' of any action bar. So the 'old' ActionBar
is now an example of a Toolbar
. If you want similar functionality, but without deprecation warnings do the following:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
toolbar.setTitle(R.string.app_name);
setSupportActionBar(toolbar);
}
You need to define the Toolbar
in your layout xml:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
With this new functionality you can create your own custom ActionBar
and let Android do the heavy lifting. Just create your own custom view that extends from Toolbar
.
Also, you should use AppCompatActivity
instead of ActionBarActivity
, it was introduced in the latest version of the appcompat library. So dont forget to update gradle
compile 'com.android.support:appcompat-v7:22.1.1'
Related Topics
Error:(1, 0) Plugin With Id 'Com.Android.Application' Not Found
Android and Facebook Share Intent
How to Add a Button Dynamically in Android
Viewpager.Setoffscreenpagelimit(0) Doesn't Work as Expected
Can Not Find a View With Findviewbyid()
How to Find Out Which Keystore Was Used to Sign an App
View Contents of Database File in Android Studio
How to Read Mms Data in Android
Httpclient Won't Import in Android Studio
What's the Best Way to Limit Text Length of Edittext in Android
How to Find Serial Number of Android Device
How to Display the Current Value of an Android Preference in the Preference Summary
How to Attach Javadoc or Sources to Jars in Libs Folder
Does Android Support Near Real Time Push Notification
Recyclerview Expand/Collapse Items
Material Effect on Button with Background Color
Android App Out of Memory Issues - Tried Everything and Still at a Loss