Activity、FragmentActivity和AppCompatActivity的区别
Activity
Activity是最基础的一个,是其它类的直接或间接父类。
Activity中只能使用系统自带的host Fragment(API Level 11中加入),对应getFragmentManager方法来控制Activity和Fragment之间的交互。
FragmentActivity
在v4包中引入FragmentActivity,FragmentActivity间接继承自Activity,并提供了对v4包中support Fragment的支持。
在FragmentActivity中必须使用getSupportFragmentManager方法来处理support Fragment的交互。也可以处理support Fragment的嵌套使用。
Known limitations:
* When using the `<fragment>` tag, this implementation can not use the parent view's ID as the new fragment's ID.
You must explicitly specify an ID (or tag) in the `<fragment>`.
AppCompatActivity
AppCompatActivity继承自FragmentActivity,同时取代了ActionBarActivity。
AppCompatActivity支持ActionBar功能,同时更推荐使用ToolBar。AppCompatActivity为支持Material Design风格控件提供了便利。
使用场景
参考stack overflow中一个回答。
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.
AppCompatActivity中AppCompat系列组件的构造及替换
AppCompatActivity中通过AppCompatDelegate来扩展Activity。AppCompatDelegate可以在任一Activity中使用,需要与合适的生命周期方法挂钩。具体可参考官方文档https://developer.android.com/reference/android/support/v7/app/AppCompatDelegate或者AppCompatDelegate的源码注释。
在AppCompatActivity的onCreate方法中,调用AppCompatDelegate(基类)的create方法创建实例,并调用delegate.installViewFactory()设置factory。
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
final AppCompatDelegate delegate = getDelegate();
delegate.installViewFactory();
delegate.onCreate(savedInstanceState);
......
super.onCreate(savedInstanceState);
}
创建AppCompatDelegate子类实例的代码如下,根据手机的安卓版本来创建不同的子类实例
private static AppCompatDelegate create(Context context, Window window,
AppCompatCallback callback) {
if (Build.VERSION.SDK_INT >= 24) {
return new AppCompatDelegateImplN(context, window, callback);
} else if (Build.VERSION.SDK_INT >= 23) {
return new AppCompatDelegateImplV23(context, window, callback);
} else if (Build.VERSION.SDK_INT >= 14) {
return new AppCompatDelegateImplV14(context, window, callback);
} else if (Build.VERSION.SDK_INT >= 11) {
return new AppCompatDelegateImplV11(context, window, callback);
} else {
return new AppCompatDelegateImplV9(context, window, callback);
}
}
以android 6.0(API 23)为例,来跟踪AppCompatDelegate实例对象的创建。AppCompatDelegateImplV23间接继承AppCompatDelegateImplV9,并实现了LayoutInflater.Factory2接口。
AppCompatDelegateImplV9的installViewFactory调用了 LayoutInflaterCompat.setFactory2,设置了自身的Factory2实现。
@Override
public void installViewFactory() {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
if (layoutInflater.getFactory() == null) {
LayoutInflaterCompat.setFactory2(layoutInflater, this);
} else {
if (!(layoutInflater.getFactory2() instanceof AppCompatDelegateImplV9)) {
Log.i(TAG, "The Activity's LayoutInflater already has a Factory installed"
+ " so we can not install AppCompat's");
}
}
}
AppCompatActivity创建view时,通过PhoneWindow->LayoutInflater(createViewFromTag)进入了其实现Factory2的onCreateView方法。可参考LayoutInflater setFactory进阶中setContentView调用流程分析
/**
* From {@link LayoutInflater.Factory2}.
*/
@Override
public final View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
// First let the Activity's Factory try and inflate the view
final View view = callActivityOnCreateView(parent, name, context, attrs);
if (view != null) {
return view;
}
// If the Factory didn't handle it, let our createView() method try
return createView(parent, name, context, attrs);
}
如果AppCompatActivity并没有设置Factory,就会调用Delegate实例的createView方法创建view。
@Override
public View createView(View parent, final String name, @NonNull Context context,
@NonNull AttributeSet attrs) {
if (mAppCompatViewInflater == null) {
mAppCompatViewInflater = new AppCompatViewInflater();
}
boolean inheritContext = false;
if (IS_PRE_LOLLIPOP) {
inheritContext = (attrs instanceof XmlPullParser)
// If we have a XmlPullParser, we can detect where we are in the layout
? ((XmlPullParser) attrs).getDepth() > 1
// Otherwise we have to use the old heuristic
: shouldInheritContext((ViewParent) parent);
}
return mAppCompatViewInflater.createView(parent, name, context, attrs, inheritContext,
IS_PRE_LOLLIPOP, /* Only read android:theme pre-L (L+ handles this anyway) */
true, /* Read read app:theme as a fallback at all times for legacy reasons */
VectorEnabledTintResources.shouldBeUsed() /* Only tint wrap the context if enabled */
);
}
根据上述代码可知,Delegate实例调用了AppCompatViewInflater的createView来完成具体view的绘制。
public final View createView(View parent, final String name, @NonNull Context context,
@NonNull AttributeSet attrs, boolean inheritContext,
boolean readAndroidTheme, boolean readAppTheme, boolean wrapContext) {
final Context originalContext = context;
// We can emulate Lollipop's android:theme attribute propagating down the view hierarchy
// by using the parent's context
if (inheritContext && parent != null) {
context = parent.getContext();
}
if (readAndroidTheme || readAppTheme) {
// We then apply the theme on the context, if specified
context = themifyContext(context, attrs, readAndroidTheme, readAppTheme);
}
if (wrapContext) {
context = TintContextWrapper.wrap(context);
}
View view = null;
// We need to 'inject' our tint aware Views in place of the standard framework versions
switch (name) {
case "TextView":
view = new AppCompatTextView(context, attrs);
break;
case "ImageView":
view = new AppCompatImageView(context, attrs);
break;
case "Button":
view = new AppCompatButton(context, attrs);
break;
case "EditText":
view = new AppCompatEditText(context, attrs);
break;
case "Spinner":
view = new AppCompatSpinner(context, attrs);
break;
case "ImageButton":
view = new AppCompatImageButton(context, attrs);
break;
case "CheckBox":
view = new AppCompatCheckBox(context, attrs);
break;
case "RadioButton":
view = new AppCompatRadioButton(context, attrs);
break;
case "CheckedTextView":
view = new AppCompatCheckedTextView(context, attrs);
break;
case "AutoCompleteTextView":
view = new AppCompatAutoCompleteTextView(context, attrs);
break;
case "MultiAutoCompleteTextView":
view = new AppCompatMultiAutoCompleteTextView(context, attrs);
break;
case "RatingBar":
view = new AppCompatRatingBar(context, attrs);
break;
case "SeekBar":
view = new AppCompatSeekBar(context, attrs);
break;
}
if (view == null && originalContext != context) {
// If the original context does not equal our themed context, then we need to manually
// inflate it using the name so that android:theme takes effect.
view = createViewFromTag(context, name, attrs);
}
if (view != null) {
// If we have created a view, check its android:onClick
checkOnClickListener(view, attrs);
}
return view;
}
在上述代码中可看到,AppCompatActivity中TextView等组件,被替代为AppCompatTextView等,从而可以利用AppCompat系列组件的特性。但是书写自定义view时,需要手动使用AppCompat组件。
参考AppCompatTextView的提示
This will automatically be used when you use TextView in your layouts and the top-level activity / dialog is provided by appcompat. You should only need to manually use this class when writing custom views.
作者:zizi192
链接:https://www.jianshu.com/p/9d590c478828
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
Activity、FragmentActivity和AppCompatActivity的区别的更多相关文章
- android中activity.this跟getApplicationContext的区别
转载: http://www.myexception.cn/android/1968332.html android中activity.this和getApplicationContext的区别 在a ...
- Android中Activity和AppcompatActivity的区别(详细解析)
转载 https://blog.csdn.net/today_work/article/details/79300181 继承AppCompatActivity的界面. 如下图所示: copy界面代码 ...
- Application、Activity Stack 和 Task的区别
Application类 Application和Activity,Service一样是Android框架的一个系统组件,当Android程序启动时系统会创建一个Application对象,用来存储系 ...
- AppCompatActivity、ActionBarActivity、FragmentActivity和Activity的区别
package com.chy.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bu ...
- Android Studio修改默认Activity继承AppCompatActivity
在Android Studio中新建Activity默认继承AppCompatActivity,感觉这点十分不爽,找了很久,终于发现在android Studio安装目录下有个模板文件,修改其中的参数 ...
- Android Studio修改默认Activity继承AppCompatActivity(转)
在Android Studio中新建Activity默认继承AppCompatActivity,感觉这点十分不爽,找了很久,终于发现在Android Studio安装目录下有个模板文件,修改其中的参数 ...
- Android零基础入门第73节:Activity初入门,创建和配置如此简单
Activity是Android应用的重要组成单元之一,也是Android应用最常见的组件之一.前面看到的示例通常都只包含一个Activity或一个AppCompatActivity,但在实际应用中这 ...
- 报错:You need to use a Theme.AppCompat theme (or descendant) with this activity.
学习 Activity 生命周期时希望通过 Dialog 主题测试 onPause() 和 onStop() 的区别,点击按钮跳转 Activity 时报错: E/AndroidRuntime: FA ...
- Android四大组件——Activity
Activity作为Android四大组件之一,也是其中最重要的一个组件.作为一个与用户交互的组件,我们可以把Activity比较成为windows系统上的一个文件夹窗口,是一个与用户交互的界面.再进 ...
随机推荐
- 以代码的方式管理quartz定时任务的暂停、重启、删除、添加等
[前言]在项目的管理功能中,对定时任务的管理有时会很常见.因为我们不能指望只在配置文件中配置好定时任务就行了,因为如果要控制定时任务的 “暂停” 呢?暂停之后又要在某个时间点 “重启” 该定时任务呢? ...
- ArrayBlockingQueue 和LinkedBlockQueue
ArrayBlockingQueue ArrayBlockingQueue是Java多线程常用的线程安全的一个集合,基于数组实现,继承自AbstractQueue,实现了BlockingQueue和S ...
- 2016.8.17上午纪中初中部NOIP普及组比赛
2016.8.17上午纪中初中部NOIP普及组比赛 链接:https://jzoj.net/junior/#contest/home/1335 本来觉得自己能考高分,但只得160分,并列第九.至少又挤 ...
- 深入浅出 Java Concurrency (24): 并发容器 part 9 双向队列集合 Deque[转]
有一段时间没有更新了.接着上节继续吧. Queue除了前面介绍的实现外,还有一种双向的Queue实现Deque.这种队列允许在队列头和尾部进行入队出队操作,因此在功能上比Queue显然要更复杂.下图描 ...
- 图解SQL的Join
原文地址:http://coolshell.cn/articles/3463.html 对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的 ...
- Activiti历史查看
流程执行完毕后,究竟去了哪里有些疑问. 虽然已完成的任务在act_ru_task和act_ru_execution表中都已被删除,但是这些数据还存在activiti的数据库中,作为历史改由Histor ...
- Nginx报错汇总
1. Nginx 无法启动解决方法 在查看到 logs 中报了如下错误时: 0.0.0.0:80 failed (10013: An attempt was made to access a ...
- linux系统下重要的分区及其作用
下面列出来的是linux系统下重要的分区及其作用/bin :bin是binary的缩写;/boot :存放启动Linux时使用的一些核心文件;/root :root(超级管理员)的用户主目录;/sbi ...
- JZOJ2368 【SDOI2011】黑白棋
题目 题目大意 在一个1*n的棋盘上,有黑棋和白棋交错分布,每次,一个人可以移动自己的ddd颗旗子. 问先手必胜的方案数. 思考历程 在一开始,我就有点要放弃的念头. 因为这题是一道博弈问题. 我是非 ...
- 廖雪峰Java10加密与安全-1数据安全-1加密与安全概念
数据安全 防窃听 防篡改 防伪造 古代加密方式: 移位密码:HELLO =>IFMMP 替代密码:HELLO=>p12,5,3 现代加密方式: 建立在严格的数学理论基础上 密码学逐渐发展成 ...