Android - startActivityForResult immediately triggering onActivityResult

AndroidAndroid Activity

Android Problem Overview


I am launching activities from the main activity in my app using the call startActivityForResult(intent, ACTIVITY_TYPE), and they are all working but one.

This one, when called, launches the activity as desired, but in the log I can see that onActivityResult() is immediately being triggered. The activity shows up but RESULT_CANCELED is immediately returned to onActivityResult().

I then interact with the activity, press a button which calls finish(), and onActivityResult() is not called this time (because apparently a result has already been returned).

Does this make sense to anyone? Has anyone seen this behavior before?

Android Solutions


Solution 1 - Android

You can't use startActivityForResult() if your activity is being launched as a singleInstance or singleTask. standard or singleTop launch mode will fix the problem.

Solution 2 - Android

Additionally make sure the intent does not have the Intent.FLAG_ACTIVITY_NEW_TASK set.

From the docs:

> This flag can not be used when the caller is requesting a result from > the activity being launched.

Solution 3 - Android

I have seen this behavior before, please make sure your destnation activity (that special activity) is not singleInstance in AndroidManifest file. If the Activity is singleInstance, then it will return RESULT_CANCELED before launched!

Solution 4 - Android

I'd also like to add that you could call an external app with:
Intent in = caller.getPackageManager().getLaunchIntentForPackage("com.your.package.here");
Which would create an intent with Intent.FLAG_ACTIVITY_NEW_TASK added by default, so call:
in.setFlags(0);
Which will clear that flag, and then you can proceed to: startActivityForResult(in, action);

Reason I'm doing this is that I have a utility app that has common functionality between a few other apps and I can keep the code changes to one location instead of worrying about multiple updates.

Solution 5 - Android

startActivityForResult() doesn't work with a singleInstance or singleTask activity in pre-lollipop version of Android. Since Android 5 it works (see this answer for more details).

Solution 6 - Android

It also triggers if you have FLAG_ACTIVITY_NEW_TASK in your intent.

Intent intent = new Intent(this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, REQUEST_CODE);

Solution 7 - Android

My problem was with the calling activity. Its declaration in the AndroidManifest had the following property:

android:noHistory="true"

Changed it to "false" and now works fine.

Solution 8 - Android

Android 4.4 has a small problem about waiting for the return at the end of the actvity closure To solve this behavior it is important to set :

  • all activities will have the same task Affinity attribute. (TaskAffinity = "[SAME STRING]")
  • launchmode=singleTop,
  • launchIntent.SetFlags(0); // for reset default Intent flags if you launch from package manager

This solution works with all version of Android

See this for taskAffinity: https://asyoulook.com/computers%20&%20internet/android-onactivityresult-being-called-instantly/1004072

Solution 9 - Android

Also, check if android:noHistory="true" on activity in Manifest, if yes, it will not work.

Solution 10 - Android

For ActivityGroup or TabHost and others, maybe the xxxActivity is a subActivity of its parent. Then the startActivityForResult can not work but the parent can get the result.

  1. call getParent().startActivityForResult() from your sub-activity

  2. your parent (the ActivityGroup) will be able to handle the onActivityResult. So I created a subclass of ActivityGroup and handled this onActivityResult.

  3. You can re-route that result back to the sub-activity if you need to. Just get the current activity by getLocalActivityManager().getCurrentActivity() . My sub-activities inherit from a custom activity so I added a handleActivityResult(requestCode, resultCode, data) in that subclass for the ActivityGroup to call.

example: http://www.cnblogs.com/relinson/archive/2012/03/25/startActivityForResult.html

Solution 11 - Android

onActivityResult() will also pass RESULT_CANCELED as the resultCode if you misspell the package or class name in the manifest file.

Solution 12 - Android

In Android Manifest set android:launchMode="singleTop" for activity you want open with result and while opening activity set flag intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

Solution 13 - Android

If you defined android:noHistory="true" in the activity in your AndroidManifest.xml, it will cause the same issue here.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTomBombView Question on Stackoverflow
Solution 1 - AndroidFalmarriView Answer on Stackoverflow
Solution 2 - AndroidJames ZhangView Answer on Stackoverflow
Solution 3 - AndroidpangcongView Answer on Stackoverflow
Solution 4 - AndroidgiannileuaniView Answer on Stackoverflow
Solution 5 - AndroidnoelicusView Answer on Stackoverflow
Solution 6 - AndroidRoger AlienView Answer on Stackoverflow
Solution 7 - Androiduser3829751View Answer on Stackoverflow
Solution 8 - AndroidAndrea Falappi - PolipoView Answer on Stackoverflow
Solution 9 - AndroidSanjuView Answer on Stackoverflow
Solution 10 - Androidfantaxy025025View Answer on Stackoverflow
Solution 11 - AndroidmusterjunkView Answer on Stackoverflow
Solution 12 - AndroidKrishView Answer on Stackoverflow
Solution 13 - AndroidfiremaplesView Answer on Stackoverflow