mContext.startActivity(intent)失败原因分析

    在android中经常要启动Activity,显示启动或者隐式启动。启动Activity都要调用mContext.startActivity(intent)方法。.startActivity(intent)为Context类的的抽象方法。

    当在Activity中调用this..startActivity(intent)来启动Activity时,this为该activity的一个实例,在Activity中实现了该方法,所以会直接调用Activity..startActivity(intent),该实现方法最终调用startActivityForResult(Intent intent, int requestCode, Bundle options)方法,源码如下:

public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
        if (mParent == null) {
            Instrumentation.ActivityResult ar =
                mInstrumentation.execStartActivity(
                    this, mMainThread.getApplicationThread(), mToken, this,
                    intent, requestCode, options);
            if (ar != null) {
                mMainThread.sendActivityResult(
                    mToken, mEmbeddedID, requestCode, ar.getResultCode(),
                    ar.getResultData());
            }
            if (requestCode >= 0) {
                // If this start is requesting a result, we can avoid making
                // the activity visible until the result is received.  Setting
                // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
                // activity hidden during this time, to avoid flickering.
                // This can only be done when a result is requested because
                // that guarantees we will get information back when the
                // activity is finished, no matter what happens to it.
                mStartedActivity = true;
            }
        } else {
            if (options != null) {
                mParent.startActivityFromChild(this, intent, requestCode, options);
            } else {
                // Note we want to go through this method for compatibility with
                // existing applications that may have overridden it.
                mParent.startActivityFromChild(this, intent, requestCode);
            }
        }
    }


如果mContext不是Activity的实例(如service,application等),则会调用其父类ContextWrapper的startActivity(intent)方法,方法如下:

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

方法中mBase为抽象类Context的实现类ContextImpl的实例,该实例实现startActivity(intent)如下:

@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);
    }

    从if语句可以看出,如果该intent的Flag不是Intent.FLAG_ACTIVITY_NEW_TASK(intent.getIFlags()&Intent.FLAG_ACTIVITY_NEW_TASK),就会抛出异常,并不启动Activity。

 结论:(1)从Activity中启动新的Activity时可以直接mContext.startActivity(intent)就好,

(2)如果从其他Context中启动Activity则必须给intent设置Flag:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ; 

mContext.startActivity(intent);

你可能感兴趣的:(android应用日报)