Difference between extending LifecycleActivity,Activity,ActionbarActivity & AppCompactActivity?
Asked Answered
A

5

70

In Android what is the main difference between extending Lifecycler Activity, Activity,ActionBarActivity & AppCompactActivity? How do these classes differ from each other in terms of usage?

Afflatus answered 15/4, 2014 at 16:33 Comment(2)
It is all explained here: developer.android.com/reference/android/support/v7/app/…Hargrave
Read more about it at here: #23045972 ActionBarActivity extends FragmentActivity and adds support for the appcompat action bar backport.Meadors
B
83
  • 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

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 ActionBarActivityis 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)

Boron answered 15/4, 2014 at 17:16 Comment(7)
"you'll lack the ActionBar on api levels below" 7State
@State The ActionBar was introduced in API level 11. Therefore, by extending Activity you'll only have it on devices running Android 3.0 Honeycomb (which is API level 11) and aboveBoron
this is not true, indeed support library is provided to support functionalities for api level lower than 11 (for instance)State
The sentence you quoted is referring the case when no support library is used, therefore you won't have actionbar on api levels <11. You'll have the original Activity designed for those api levelsBoron
Thumbs up to this answer. Of course there is enough documentation to read till the end of our days, what we need sometimes isa nutshell definition of things.Kaleb
@Boron please update your answer and add Lifecycle Activity to it :)Afflatus
@UmerKiani I am not sure whether that's a good approach for a question-answer system as there are plenty of activities that could be included in the question and it would start looking as an article or documentation - probably something out of the SO scope?Boron
D
7

If you look carefully, you will see this

public class ActionBarActivity extends FragmentActivity
implements ActionBarDrawerToggle.DelegateProvider TaskStackBuilder.SupportParentable

Here you can read about FragmentActivity: http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html

And differences between Activity and FragmentActivity: Difference between Activity and FragmentActivity

Also, there are some new themes for styling actionBar... https://developer.android.com/training/basics/actionbar/styling.html

Actionbar is introduced in API level 11. com.android.support:appcompat-v7:+ is a support library which allows you to have an ActionBar in your app for devices running on Android 3.0 or below. So, if you need actionbar below api level 11 your Activity needs to extend ActionBarActivity.

If you are targetting api level 11 and above then you don't need to extend ActionBarActivity and reference AppCompat. You can simply extend Activity and you will have actionabr by default.

Android Studio default project includes it automatically in dependencies and extends ActionbarActivity instead of Activity in order to use it.

Dribble answered 15/4, 2014 at 16:55 Comment(0)
B
4

The ActionBarActivity or the SupportActionBarActivity have additional methods and properties that are not in a generic Activity. for example methods for adding tabs are present in the ActionBarActivity and not in a generic Activity.

The major difference being you don't get an ActionBar on a generic Activity.

Burin answered 15/4, 2014 at 16:51 Comment(5)
What if one is not interested to use actionbar? Does it has some performance improvements over Simple Activity or just its better to use only because of actionbar ?Afflatus
IDK if actionbaractivity has performance improvements versus non activityBurin
Thanks. I am still bit confused of what to use in my future apps and what is more recomended.Afflatus
If you dont want to use ActioBarActivity, you should use FragmentActivity... Check my post, it describes the logic behind the ActionBarActivity.Dribble
Don't worry about what to use its very easy to add an action bar to an existing app. Maybe 10 minutes You just change the code to extends actionbaractivity then fix the imports and any other nuance errors.Burin
B
1

ActionBarActivity just has more support libraries and better usage of the newer themes available from api 11.

"In its most basic form, the action bar displays the title for the activity and the app icon on the left. Even in this simple form, the action bar is useful for all activities to inform users about where they are and to maintain a consistent identity for your app."

Bandeau answered 15/4, 2014 at 16:53 Comment(0)
A
1

You are using Android support library When you come to to the Actionbaractivity . so the uses of Support library is your application can be suport for maximum number of devices. Support library gives to your application the power of Backward compatibilty. Actionbaractivity gives you the mulitiple feature like Actionbardrawer toggle etc.. there are more support libraries available. see this link .. and share to your friends...https://developer.android.com/tools/support-library/index.html

Agora answered 29/12, 2014 at 1:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.