What's the enhancement of AppCompatActivity over ActionBarActivity?

94,930

Solution 1

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".

Solution 2

It's mostly a name change: ActionBarActivity doesn't really describe everything it now does. You can safely use ActionBarActivity if you wish to. Think of it like a symlink.

Solution 3

The AppCompat Support Library started with humble, but important beginnings: a single consistent Action Bar for all API 7 and higher devices. In revision 21, it took on new responsibility: bringing material color palette, widget tinting, Toolbar support, and more to all API 7+ devices. With that, the name ActionBarActivity didn’t really cover the full scope of what it really did.

http://android-developers.blogspot.it/2015/04/android-support-library-221.html

Solution 4

AppCompatActivity was introduced into Android-SDK since the release of android support appcompat library.

AppCompatActivity is the direct child class of FragmentActivity of support v4 and the direct parent class of ActionBarActivity.

AppCompatActivity is the 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.

As for support v7 appcompat library, it adds support for the Action Bar user interface design pattern. This library includes support for material design user interface implementations.

Here are a few of the key classes included in the v7 appcompat library:

  • ActionBar - Provides an implementation of the action bar user interface pattern.
  • AppCompatActivity - Adds an application activity class that can be used as a base class for activities that use the Support Library action bar implementation.
  • AppCompatDialog - Adds a dialog class that can be used as a base class for AppCompat themed dialogs.
  • ShareActionProvider - Adds support for a standardized sharing action (such as email or posting to social applications) that can be included in an action bar.

After you download the Android Support Libraries, this library is located in the /extras/android/support/v7/appcompat/ directory.

Solution 5

Previously the only entry point into AppCompat was through the now deprecated ActionBarActivity class. Unfortunately this forced you into using a set Activity hierarchy which made things like using PreferenceActivity impossible.

see chris banes's support-libraries-v22-1-0 for more info

Share:
94,930

Related videos on Youtube

SilentKnight
Author by

SilentKnight

Study the Major of Software Engineering in Wuhan University from Sep, 2009 to Jun, 2013. Android R&D Engineer with the Hero Complex @ Big Data Innovation Centre, CreditEase. Google, or Android Technology Enthusiast exactly. Hollywood Movies, Grammy Music, Basketball Games Lover. English Language and Literature Lover. Loyal Follower of DC Heroes && Big Fun of Marvel Universe.

Updated on September 02, 2020

Comments

  • SilentKnight
    SilentKnight almost 4 years

    android.support.v7.app.AppCompatActivity was added into the latest v7 support library as a new feature yesterday.

    It is said that ActionBarActivity has been deprecated in favor of the new AppCompatActivity and that AppCompatActivity is base class for activities that use the support library action bar features. So, what are new features of AppCompatActivity over ActionBarActivity? What enhancements do AppCompatActivity have over ActionBarActivity? And what are advantages of AppCompatActivity? Could somebody supply a few samples?

    PS: what surprised me most is that AppCompatActivity which is extended from android.support.v4.app.FragmentActivity is the direct parent class of ActionBarActivity! I mean actually now that ActionBarActivity can do anything that AppCompatActivity can do, why did Android pushed out the latter?

    Meanwhile, I saw a blog post that states: "It's not a rename from ActionBarActivity to AppCompatActivity, the internal logic of AppCompat is available via AppCompatDelegate", so what's the "internal logic" of AppCompat? What can AppCompatDelegate do? Could somebody post some codes about this?

    • CommonsWare
      CommonsWare about 9 years
    • SilentKnight
      SilentKnight about 9 years
      I have read it, it is a introduction, too shallow.
    • Pankaj Kumar
      Pankaj Kumar about 9 years
      AppCompatActivity is parent class of ActionBarActivity. And for more idea you need to look into code.
    • SilentKnight
      SilentKnight about 9 years
      Yes, I noticed this.
  • SilentKnight
    SilentKnight about 9 years
    It's Chris's blog, very appreciated.
  • SilentKnight
    SilentKnight about 9 years
    AppCompatActivity is the direct parent class of ActionBarActivity, now that ActionBarActivity can do anything that AppCompatActivity can do, why Android pushed out the later?
  • CommonsWare
    CommonsWare about 9 years
    @SilentKnight: As Chris wrote, it is mostly a name change. Quoting the Android Developers Blog: "The AppCompat Support Library started with humble, but important beginnings: a single consistent Action Bar for all API 7 and higher devices. In revision 21, it took on new responsibility: bringing material color palette, widget tinting, Toolbar support, and more to all API 7+ devices. With that, the name ActionBarActivity didn’t really cover the full scope of what it really did."
  • david.schreiber
    david.schreiber about 9 years
    It is not safe to use deprecated classes, since they are subject to removal in future versions. Although you can easily replace all references to ActionBarActivity when it is dropped from the support library (e.g. when updating your app) I would recommend to avoid using it from now on.
  • Rakesh L
    Rakesh L about 8 years
    Good Example .. Thank you @ sergej shafarenka
  • Ravindra Kushwaha
    Ravindra Kushwaha over 7 years
    Can u please tell me that what happen when i will use Activity in place of Appcompactivity? I have searched but did not get the solution.Please help me
  • Ravindra Kushwaha
    Ravindra Kushwaha over 7 years
    ..Can u please tell me that what happen when i will use Activity in place of Appcompactivity? I have searched but did not get the solution.Please help me
  • Taylan
    Taylan over 7 years
    What about android.support.v4.app.ActivityCompat? Is this just the old name of AppCompatActivity?