开发者

onActivityResult do not fire if launch mode of activity is singleInstance

开发者 https://www.devze.com 2023-02-13 16:02 出处:网络
I have an Activity which is basically my main activity and its launch mode is single instance. But because of singleInstance, the onActivityResult() callback does not fire. And ifI change the launch m

I have an Activity which is basically my main activity and its launch mode is single instance. But because of singleInstance, the onActivityResult() callback does not fire. And if I change the launch mode in my manifest file to any other mode it works fine.

Can you explain why this callback is not w开发者_高级运维orking?


I believe that the problem is that singleInstance doesn't let the callee activity to exist in the same task as the caller, hence it can't return the value to the caller.

Consider using singleTask instead:

singleTask

The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.

singleInstance

Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.

http://developer.android.com/guide/topics/manifest/activity-element.html


If an activity is singleInstance, it will be the only one in the task so it always be the top of the task. So when you call startActivityForResult it will fire the callback method immediately.


A "singleInstance" activity, permits no other activities to be part of its task. It's the only activity in the task. If it starts another activity, that activity is assigned to a different task. The activity is always the single and only member of its task.
I think onActivityResult will not work with singleInstance


You can't use singleInstance or singleTask with startActivityForResult method.

Standard mode or singleTop launch mode will fix the problem.


Android Source Code

Check "ActivityStarter.computeLaunchingTaskFlags()" method:

            } else if (mSourceRecord.launchMode == LAUNCH_SINGLE_INSTANCE) {
            // The original activity who is starting us is running as a single
            // instance...  this new activity it is starting must go on its
            // own task.
            mLaunchFlags |= FLAG_ACTIVITY_NEW_TASK;

That's the reason why NEW_TASK flag is added when your original activity with single instance launch mode.

More Source Code

Check "ActivityStarter.sendNewTaskResultRequestIfNeeded()" method:

        if (sourceStack != null && (mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
        // For whatever reason this activity is being launched into a new task...
        // yet the caller has requested a result back.  Well, that is pretty messed up,
        // so instead immediately send back a cancel and let the new task continue launched
        // as normal without a dependency on its originator.
        Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
        sourceStack.sendActivityResultLocked(-1 /* callingUid */, mStartActivity.resultTo,
                mStartActivity.resultWho, mStartActivity.requestCode, RESULT_CANCELED,
                null /* data */);

That's the reason why FLAG_ACTIVITY_NEW_TASK always immediately return RESULT_CANCELED.


An example of my application can benefit someone:

  • In the manifest file, my settings are:

<application android:launchMode="singleTask"...>

  • I use one java class, only as a launcher(it does not show Activity). In the manifest, it is set to:

<activity android:launchMode="singleTop" ...>

  • Also, other Activities are set the same:

<activity android:launchMode="singleTop" ...>

When a user sends an application to the background, by clicking on the application icon or restoring the application from the background, the last started Activity is returned unchanged, even though they were all started with startActivityForResult() from launcher.

0

精彩评论

暂无评论...
验证码 换一张
取 消