Android - startActivityForResult immediately triggering onActivityResult
Asked Answered
D

13

155

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?

Dibrin answered 27/10, 2011 at 1:38 Comment(0)
S
324

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.

Saturday answered 27/10, 2011 at 1:50 Comment(3)
singleTop seems fine for me, singleTask however was causing this issue and hence singleInstance would definitely do tooAbreaction
In my case, did not defined any class as an singleInstance or singleTop, but still it is happening...any suggestion ?Taylor
Note that if the intent-filter on the target activity marks it as category LAUNCHER, the activity is launched in a new task by default. This can cause the immediately-canceled behavior, even if it is not marked singleTask or singleInstance.Poulard
A
125

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.

Anallise answered 31/1, 2013 at 22:59 Comment(5)
This fixed the problem in my case. The problem was not caused by singleInstance, singleTop nor singleTask.Erotica
@Diana, do you remember what you used instead of Intent.FLAG_ACTIVITY_NEW_TASK ?Seiden
@Seiden Sorry, it's been a long time, I wish I could help but I don't remember and I can't access that code any more.Erotica
Yes, fixed it. startActivityForResult should not be used with Intent.FLAG_ACTIVITY_NEW_TASKName
@Name what is the intent flag you used. I am having the same issue with gps launcherMohamed
H
21

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!

Hescock answered 27/10, 2011 at 1:58 Comment(0)
M
21

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.

Misjoinder answered 19/6, 2014 at 17:6 Comment(4)
I know this is old, but this was so absolutely invaluable to me that I wanted to say thanks to the user, and point out for anyone jumping between activities that this is freaking gold! Saved my ass during a hackathon ;)Attractant
This is what needs more attention. It is the best solution and works perfectly for me. You saved me a lot of time, thank you so much.Oestriol
Up-voting this answer as this was the exact issue I was having this morning! Thanks for the solution!Hunger
Thanks. intent.setFlags(0) solved the problem. Now the second app starts ok and returns result to the calling appToastmaster
G
5

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).

Globeflower answered 21/9, 2017 at 19:28 Comment(0)
K
5

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);
Kalgan answered 22/9, 2017 at 19:49 Comment(1)
Thanks. I added FLAG_ACTIVITY_NEW_TASK in my activity.Scherer
N
1

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.

Nicolina answered 29/4, 2016 at 22:43 Comment(0)
G
1

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

Gent answered 7/12, 2017 at 11:30 Comment(0)
C
1

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

Cabalist answered 26/11, 2018 at 6:35 Comment(0)
T
0

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

Tahmosh answered 20/7, 2013 at 14:41 Comment(1)
What do you think about this special case when calling default web brower : codeproject.com/Questions/990063/…Frum
R
0

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

Rabat answered 12/5, 2015 at 2:9 Comment(0)
W
0

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

Wamble answered 26/11, 2016 at 12:7 Comment(0)
B
0

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

Bartlett answered 11/9, 2018 at 10:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.