Android中Activity的startActivity和Context的startActivity有什么不同

4次阅读

共计 2209 个字符,预计需要花费 6 分钟才能阅读完成。

原文:http://tryenough.com/android-…

在使用中的不同
1. 在 Activity 中跳转到其他的 Activity 时,两种使用方法是一样的:
this.startActivity(intent);
context.startActivity(intent);
2. 从非 Activity(例如从其他 Context 中)启动 Activity 则必须给 intent 设置 Flag:FLAG_ACTIVITY_NEW_TASK:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ;
mContext.startActivity(intent);
探究一下为什么会有这方面的差异

原文:http://tryenough.com/android-…

首先看下 Activity 和 context 的继承关系:

要知道 Activity 和 context 中的 StartActivity 有什么区别,我们看下它们分别是怎么实现 startActivity 函数的:
原文:http://tryenough.com/android-…

1.Context 是抽象类,它的 startActivity 函数是抽象方法:
public abstract void startActivity(@RequiresPermission Intent intent);
2.ContextWrapper 类只是调用了 Context 的实现:
@Override
public void startActivity(Intent intent) {
mBase.startActivity(intent);
}
3.ContextThemeWrapper 中没有实现此方法
4.Activity 中:
@Override
public void startActivity(Intent intent, @Nullable Bundle options) {
if (options != null) {
startActivityForResult(intent, -1, options);
} else {
// Note we want to go through this call for compatibility with
// applications that may have overridden the method.
startActivityForResult(intent, -1);
}
}
由此可见,在 Activity 中无论是使用哪一种 startActivity 方法都会调用到 Activity 自身的方法,所以是一样的。
原文:http://tryenough.com/android-…

然而在其他的 Context 子类,例如 ContextImpl.java 中的实现,会检查有没有设置 Flag:FLAG_ACTIVITY_NEW_TASK,否则会报错:
@Override
public void startActivity(Intent intent, Bundle options) {
warnIfCallingFromSystemProcess();

// Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is
// generally not allowed, except if the caller specifies the task id the activity should
// be launched in. A bug was existed between N and O-MR1 which allowed this to work. We
// maintain this for backwards compatibility.
final int targetSdkVersion = getApplicationInfo().targetSdkVersion;

if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
&& (targetSdkVersion < Build.VERSION_CODES.N
|| targetSdkVersion >= Build.VERSION_CODES.P)
&& (options == null
|| ActivityOptions.fromBundle(options).getLaunchTaskId() == -1)) {
throw new AndroidRuntimeException(
“Calling startActivity() from outside of an Activity ”
+ ” context requires the FLAG_ACTIVITY_NEW_TASK flag.”
+ ” Is this really what you want?”);
}
mMainThread.getInstrumentation().execStartActivity(
getOuterContext(), mMainThread.getApplicationThread(), null,
(Activity) null, intent, -1, options);
}
原文:http://tryenough.com/android-…

这也就是为什么 Activity 的 startActivity 和 Context 的 startActivity 会有上面那些使用上的不同的原因了。

正文完
 0