What's the difference between the Start Activity of Activity in Android and the Start Activity of Contentext?

Keywords: Android Java

Original text: http://tryenough.com/android-...

Differences in use

1. When jumping to other Activities in Activity, the two methods are the same:

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

2. Starting an Activity from a non-Activity (e.g. from other contexts) requires setting Flag to intent: FLAG_Activity_NEW_TASK:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ; 
mContext.startActivity(intent);

Explore why there are differences in this respect

Original text: http://tryenough.com/android-...

First, look at the inheritance relationship between Activity and context:

To know the difference between StartActivity in Activity and context, let's see how they implement the startActivity function respectively:

Original text: http://tryenough.com/android-...

1.Context is an abstract class whose startActivity function is an abstract method:

public abstract void startActivity(@RequiresPermission Intent intent);

2. The ContextWrapper class simply calls the Context implementation:

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

3. This method is not implemented in ContextTheme Wrapper

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

Thus, no matter which startActivity method is used in Activity, it will call the method of Activity itself, so it is the same.

Original text: http://tryenough.com/android-...

However, implementations in other contextext subclasses, such as Context Impl. java, check if Flag:FLAG_ACTIVITY_NEW_TASK is set, otherwise errors will be reported:

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

Original text: http://tryenough.com/android-...

That's why the startActivity of Activity and the startActivity of Contentext have different uses.

Posted by chriskl on Thu, 07 Mar 2019 00:27:23 -0800