Difference Between Extending Lifecycleactivity,Activity,Actionbaractivity & Appcompactactivity

Difference between extending LifecycleActivity,Activity,ActionbarActivity & AppCompactActivity?

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

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.

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

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

Difference between ActionBarActivity and Fragment Activity

FragmentActivity is the base class for support based fragments. So you will be using Fragment from support library below api level 11 in which case your Activity needs to extend FragmentActivity.

 ↳  android.support.v4.app.FragmentActivity
↳ android.support.v7.app.ActionBarActivity

You will use ActionBarActivity when you need actionbar below API level 11 by using AppCompat library. In this case your Activity extends ActionBarActivity.

As you see ActionBarActivity extends FragmentActivity

Why my first android MainActivity class extends ActionBarActivity instead of Activity?

It depends on the type of Activity you choose when creating a new project via the wizard (File>New>Android Application Project). If you choose Empty Activity at the Create Activity page, you should get a starting point similar to the tutorials.

Difference between Activity and FragmentActivity

A FragmentActivity is a subclass of Activity that was built for the Android Support Package.

The FragmentActivity class adds a couple new methods to ensure compatibility with older versions of Android, but other than that, there really isn't much of a difference between the two. Just make sure you change all calls to getLoaderManager() and getFragmentManager() to getSupportLoaderManager() and getSupportFragmentManager() respectively.

Android Fragments : support library crashes on extending Activity class

You have to choose whether you use classes from the support library or not. If you do, you have to use classes that are compatible with each other. FragmentActivity and ActionBarActivity are part of the support library, hence they support android.support.v4.app.Fragment. Activity is not from the support lib, so it supports android.app.Fragment.

Basically, Activity and ActionBarActivity do the same things. There are minor differences between the 2, the main one being the method getFragmentManager() in Activity being replaced by getSupportFragmentManager() in the support library. Other methods that differ are usually prefixed with 'support' in ActionBarActivity.



Related Topics



Leave a reply



Submit