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?
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)
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.
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.
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."
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
Related
Given the following:
Android documentation says :
AppCompatActivity - Adds an application activity class that can be used as a base class for activities that use the Support Library action bar implementation.
I'm not considering adding action bar to my activity
I need some material design widgets, which I can control only through AppCompat or material theme, but the latest need API >= 21, which is not my case.
I tested Theme.AppCompat.Light.NoActionBar with Activity class and it works well.
--
Is there an issue with using Activity class with AppCompat theme in my case?
The AppCompat library is intended to make compatibility with olders API, so the Theme and all components may work well in older systems.
I think the only concern is to always use the AppCompat elements and not the regular ones.
Example, use AppCompatEditText, AppCompatTextView, etc... And always refers to they with the AppCompat (AppCompatEditText editText;)
I have used a lot the support library and not have others issues, considering the visual elements may be a little different when using an API minor than 21
No.There is not issues with AppcompactActivity & support libaray. You can refere this link
To gain more rich & amazing look go with support library,Try to do material design
I am building app with the sdk levels (min 19- max 22), And i want to use material design in my app. Should i extend Activity or AppcompatActivity? What is the difference? I know AppcompatActivity is support library for backward compatability, Other than that is there any main difference between both?
Extending Activity is deprecated so you should use AppCompatActivity.
Ya we can.Just add the library in the Gradle file and use it in the activity.
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 code about this?
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".
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.
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
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.
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
The latest release of android support library, 22.1, deprecates the ActionBarActivity in favor of AppCompatActivity, which promises to bring a single consistent ActionBar for all devices starting with API Level 7 and above
I have a problem with ActionBar.
I set minsdk in AndroidManifest to 14. Next I create Activity (BlankActivity), thats create MyACtivity class which extend ActionBarActivity and import android.support.v7.app.ActionBarActivity. I think that this library need only if you use sdk level 7 or lower.
This import correctly? Or i need use another extend class?
And I try change extend class to Activity, but its does not create ActionBar on Activity.
How add ActionBar on this Activity?
P.S. I was misled, because on my sdk function getActionBar must work, its NullPointer, because my app use this strange import. A can use getSupportActionBar, but its strange use support library for sdk 7 to create Application for sdk 14 or higher.
P.S.S. Thanks!!
If anybody have same problen, there is some links about toolbar:
http://www.101apps.co.za/index.php/articles/using-toolbars-in-your-apps.html
Why was ActionBarActivity deprecated
I have a problem with ActionBar.
Who doesn't ;)
I think that this library need only if you use sdk level 7 or lower.
The appcompat-v7 library used to backport API 14 Action Bar to platforms below that. APIs 7 through 13 used this reimplementation, APIs from 14 used native Action Bar.
Since Lollipop the appcompat-v7 always uses it's own implementation of Action Bar and backports Material theme from Lollipop.
This import correctly? Or i need use another extend class?
To have the Action Bar with appcompat-v7 your activity class must extend AppCompatActivity (previously ActionBarActivity) and it's theme must descend from Theme.AppCompat.* family.
And I try change extend class to Activity, but its does not create ActionBar on Activity.
Native activities on Lollipop don't have any Action Bar by default. You would supply it by having a Toolbar widget in your layout and calling setActionBar(Toolbar). Similar approach can also be used with appcompat-v7 (if you use a theme without default action bar) by calling setSupportActionBar(Toolbar).
How add ActionBar on this Activity?
[...] but its strange use support library for sdk 7 to create Application for sdk 14 or higher.
It's perfectly OK, the goal is to make the app look the same from API 7 to API 22. Appcompat-v7 now backports not only the Action Bar but Material theme as well.
It's better you start with a working example. Just check in you android sdk installation for the folder \samples\android-21\ui\ActionBarCompat-Basic.
The use of the support library is correct, just follow the ActionBar Developer guide here.
I have an existing app with a minSdk of 14, now targeting 21, and I would like to use Theme.AppCompat to get the "Material look" throughout. This app also uses ActionBar tabs and ViewPager (FragmentPagerAdapter, etc.). I was previously using the native ActionBar and Fragment classes, but in order to use AppCompat from support.v7 it looks like I also have to use support.v7.app.ActionBar and related classes.
The issue I then ran into is that some support library ActionBar classes, such as the ActionBar.TabListener abstract interface, define methods that require the support library version of Fragments (support.v4.app.Fragment, et al). So now I'm faced with extensive modifications throughout the app to import the support library classes, change to getSupportFragmentManager, etc.
Is this the only route I can take for using the AppCompat themes for V14-21 -- switch entirely to the support library for both the action bar and fragments? Do some of these classes pass thru to the native classes at execution time?