activity的startActivity和context的startActivity有什么不同?

原文链接:https://blog.csdn.net/u011694328/article/details/53355361

1. 首先,Context 类有一个 abstract 方法

/**
     * Same as {@link #startActivity(Intent, Bundle)} with no options
     * specified.
     *
     * @param intent The description of the activity to start.
     *
     * @throws ActivityNotFoundException  
     *`
     * @see #startActivity(Intent, Bundle)
     * @see PackageManager#resolveActivity
     */
    public abstract void startActivity(@RequiresPermission Intent intent);

2. Context 的一个子类叫 ContextWrapper ,ContextWrapper 虽然实现了 startActivity (Intent) 方法,但是很简单,因为需要实例化,所以必须要实现父类中的 abstract 方法。

@Override
 public void startActivity(Intent intent) {
     mBase.startActivity(intent);
 }

3. ContextWrapper 有一个子类叫 ContextThemeWrapper ,这个类并没有实现startActivity(Intent)方法。

4. Activity 的定义如下:

public class Activity extends ContextThemeWrapper
     implements LayoutInflater.Factory2,
     Window.Callback, KeyEvent.Callback,
     OnCreateContextMenuListener, ComponentCallbacks2,
     Window.OnWindowDismissedCallback {
}

其实现了 startActivity(Intent) 方法:

/**
 * Same as {@link #startActivity(Intent, Bundle)} with no options
 * specified.
 *
 * @param intent The intent to start.
 *
 * @throws android.content.ActivityNotFoundException
 *
 * @see {@link #startActivity(Intent, Bundle)}
 * @see #startActivityForResult
 */
 @Override
 public void startActivity(Intent intent) {
     this.startActivity(intent, null);
 }
 @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 中是等价的。

this.startActivity(intent);
context.startActivity(intent);

另外Context 的子类也有其他实现startActivity的,比如ContextImpl.java,这时候就需要一个flag:FLAG_ACTIVITY_NEW_TASK ,否则就会抛出异常。

@Override
 public void startActivity(Intent intent, Bundle options) {
     warnIfCallingFromSystemProcess();
     if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
         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);
 }

至于为什么非 Activity 实现的 startActivity 方法需要加这个 flag , 是因为在 Activity 的 startActivity的实现中,会判断如果没有这个 flag , 就会自动把这个新的 Activity 加到现有的 task 里面。而其他的 Context 或者其子类的实现中, 是没有这种判断的, 所以需要使用者指定这个 flag ,以便AMS 将其加入到其自己的 task 中。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值