Search code examples
androidandroid-activityandroid-actionbarextendsappcompatactivity

Difference between "Java class extends" "AppcompatActivity" vs "Activity" vs "ActionBar" in Android?


Android - I am going to develop an app which API is between API 15 and the latest API.
But I have one question regarding "Java class extends".

What is the difference between:

  1. Java class extends AppcompatActivity, and

  2. Java class extends Activity, and

  3. Java class extends ActionBarActivity.

If someone give a clear definition, I will definitely tick the answer as accepted for my question.


Solution

    • extending ActionBarActivity gives you the ActionBars functionality on every API level >= 7
    • by extending Activity you can avoid adding additional projects/libraries to your project but you'll lack the ActionBar 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 extending AppCompatActivity class) is a safe to use backward compatibility class. Its deprecation is just a hint for you to use new AppCompatActivity directly instead. AppCompatActivity is a new, more generic implementation which uses AppCompatDelegate 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.