154

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?

stkent
  • 19,131
  • 14
  • 82
  • 102
TomBomb
  • 3,116
  • 5
  • 29
  • 40

13 Answers13

323

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.

stkent
  • 19,131
  • 14
  • 82
  • 102
Falmarri
  • 46,415
  • 39
  • 146
  • 189
  • 2
    Thank both of you, I had this in my manifest file and had totally forgotten about it – TomBomb Oct 27 '11 at 03:14
  • 9
    WOW! This seems like an obvious thing to add to the javadocs! I wasted a whole evening trying to get this to work, and this SO answer reminded me that my Activity was set to singleTop. – swooby Mar 14 '13 at 15:29
  • 20
    `singleTop` seems fine for me, `singleTask` however was causing this issue and hence `singleInstance` would definitely do too – darnmason Jun 18 '13 at 13:39
  • 1
    holy sh**, you make my day. Thanks ! – Hrk Jul 16 '13 at 09:49
  • 4
    I also spent way too long debugging this! Thanks!! – suomi35 Jul 24 '13 at 16:48
  • Hats off to u... I faced above problem but was confused and curious about knowing the reason. – Sandeep Aug 14 '13 at 09:08
  • This is exactly what I'm looking for. You guys rock! – Vaeianor Apr 08 '14 at 17:47
  • 6
    In my case, did not defined any class as an singleInstance or singleTop, but still it is happening...any suggestion ? – CoDe Aug 27 '14 at 14:42
  • I have checked manifest for any keys like this, no result. Also checked activity for any falg, no result. In my code I am calling default web browser with URL. and it do not fire onActivtyResult, Why??? this is my code >> http://www.codeproject.com/Questions/990063/Why-onActivityResult-can-not-catch-result-from-bro?arn=1 – Ahmad Ebrahimi May 10 '15 at 12:35
  • @AhmadEbrahimi This question is 3 and a half years old. Ask your own question. – Falmarri May 14 '15 at 05:24
  • I have the same problem – sonida Oct 18 '15 at 15:49
  • I wasted my 2 days on it. – Anshul Tyagi Mar 03 '16 at 11:28
  • 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`. – Alex Jordan Sep 15 '16 at 19:16
121

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.

Vadim Kotov
  • 7,766
  • 8
  • 46
  • 61
James Zhang
  • 1,669
  • 1
  • 13
  • 8
  • 3
    This fixed the problem in my case. The problem was not caused by singleInstance, singleTop nor singleTask. – Diana Dec 04 '13 at 16:16
  • You are a mint (A life saver). This was my exact problem when I wanted the user to capture an image in a Fragment – kyay10 Feb 16 '19 at 20:12
  • @Diana, do you remember what you used instead of `Intent.FLAG_ACTIVITY_NEW_TASK` ? – Keselme Aug 06 '19 at 09:05
  • @Keselme 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. – Diana Aug 06 '19 at 15:33
  • Yes, fixed it. startActivityForResult should not be used with Intent.FLAG_ACTIVITY_NEW_TASK – hanilozmen Jun 15 '20 at 08:58
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!

naXa stands with Ukraine
  • 30,969
  • 16
  • 175
  • 237
pangcong
  • 211
  • 1
  • 2
20

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.

giannileuani
  • 442
  • 4
  • 9
  • 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 ;) – Wes Winn Sep 26 '14 at 18:35
  • 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. – Aritra Roy Aug 25 '15 at 05:22
  • Up-voting this answer as this was the exact issue I was having this morning! Thanks for the solution! – Will Johnson Apr 07 '17 at 08:43
  • Thanks. intent.setFlags(0) solved the problem. Now the second app starts ok and returns result to the calling app – Niaz Dec 02 '17 at 14:45
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).

noelicus
  • 13,693
  • 2
  • 89
  • 105
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);
Roger Alien
  • 2,972
  • 1
  • 36
  • 46
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.

user3829751
  • 692
  • 6
  • 20
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

adiga
  • 31,610
  • 8
  • 53
  • 74
1

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

Sanju
  • 115
  • 1
  • 13
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

Kyle Falconer
  • 7,954
  • 6
  • 49
  • 68
fantaxy025025
  • 799
  • 7
  • 6
  • What do you think about this special case when calling default web brower : http://www.codeproject.com/Questions/990063/Why-onActivityResult-can-not-catch-result-from-bro?arn=1 – Ahmad Ebrahimi May 10 '15 at 12:32
0

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

naXa stands with Ukraine
  • 30,969
  • 16
  • 175
  • 237
musterjunk
  • 322
  • 2
  • 13
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);

Krish
  • 556
  • 8
  • 18
0

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

firemaples
  • 1,471
  • 12
  • 18