Android中Activity的startActivity和Context的startActivity有什么不一样

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

在使用中的不一样

1.在Activity中跳转到其余的Activity时,两种使用方法是同样的:java

this.startActivity(intent);
context.startActivity(intent);
复制代码

2.从非 Activity (例如从其余Context中)启动Activity则必须给intent设置Flag:FLAG_ACTIVITY_NEW_TASK:android

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ; 
mContext.startActivity(intent);
复制代码

探究一下为何会有这方面的差别


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

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

 

 

要知道Activity和context中的StartActivity有什么区别,咱们看下它们分别是怎么实现startActivity函数的:ide

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

1.Context是抽象类,它的startActivity函数是抽象方法:函数

public abstract void startActivity(@RequiresPermission Intent intent);
复制代码

2.ContextWrapper类只是调用了Context的实现:ui

@Override
public void startActivity(Intent intent) {
    mBase.startActivity(intent);
}
复制代码

3.ContextThemeWrapper中没有实现此方法this

4.Activity中:spa

@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自身的方法,因此是同样的。code

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

然而在其余的Context子类,例如ContextImpl.java中的实现,会检查有没有设置Flag:FLAG_ACTIVITY_NEW_TASK,不然会报错:blog

@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-startActivity

这也就是为何Activity的startActivity和Context的startActivity会有上面那些使用上的不一样的缘由了。

相关文章
相关标签/搜索