Android中Activity與AppCompatActivity的理解
阿新 • • 發佈:2019-01-23
package com.chy.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; /* * support v4 FragmentActivity 相容2.x模式下使用Fragment * support v7 AppCompatActivity 相容2.x模式下使用Fragment和ActionBar,ActionBarActivity是AppCompatActivity過時產品 * 如果3.0以上直接繼承Activity,便可使用Fragment和ActionBar*/ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Activity 發展到3.0(大概)之後,可以使用fragment了,但是support v4 提供了1.6~3.0的fragment相容,所以如果需要用相容版的fragment,則需要繼承support v4提供的FragmentActivity。
而後一點點時間之後,3.0(大概)出現的ActionBar也被向前支援了,這次是出現在support v7裡,如果需要使用相容版的actionbar,則繼承support v7提供的ActionBarActivity(它是繼承FragmentActivity的)。
再然後也就是去年年底到今年,5.0提供了很多很多新東西,於是support v7也更新了,出現了AppCompatActivity , 具體功能請自行查詢。
================================================================================================================================================================
以下內容來自Googlehttp://stackoverflow.com/questions/31297246/activity-appcompatactivity-fragmentactivity-and-actionbaractivity-when-tous
=======================================================================================================================================
Activity
is the base class of all other activities, I don't think it will be deprecated. The relationship among them is:
Activity
<<<FragmentActivity
<<< AppCompatActivity
<<< ActionBarActivity
'<<<' means inheritance here. The reference said ActionBarActivity
is deprecated, use AppCompatActivity
instead.
So basically, using AppCompatActivity
is always the right choise. The differences between them:
Activity
is the basic one.- Based on
Activity
,FragmentActivity
provides the ability to useFragment
. - Based on
FragmentActivity
,AppCompatActivity
provides features toActionBar