onActivityResult() called prematurely
Asked Answered
D

5

94

I start the Activity (descendant of PreferenceActivity) from my worker activity as follows:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1458)
        loadInfo();
}

void showSettingsDialog()
{
    startActivityForResult(new Intent().setClass(this, MyConfigure.class), 1458);
}

MyConfigure class does NOT have any setResult() calls. In fact, MyConfigure class doesn't have any code except OnCreate() where it loads preferences using addPreferencesFromResource.

Now onActivityResult is called with requestCode of 1458 prematurely, right after MyConfigure activity is run. Tested on 1.6 and 2.1 emulators as well as 2.1 device. Is there a call to setResult() buried somewhere in PreferenceActivity? Or how else can this premature call be explained?

Devilmaycare answered 28/7, 2010 at 16:15 Comment(6)
An activity doesn't end on setResults(), it ends on finish(). Can you show the onCreate method of your MyConfigure activity?Porker
Right, it doesn't. However, something does call setResult() ahead of time and I am wondering, what it is. The code of onCreate is trivial: public class MyConfigure extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); } }Hernando
how do you think to know that setResult is called?Minnieminnnie
That is exactly what I created the question for. To find out, why onActivityResult is called prematurely.Hernando
What does the logcat output say around this period? Specifically the "ActivityManager" tag, which shows which Intents are being called.Nickens
Is anyone else experiencing this problem when both activities (the one that's launching and the one that's being launched) are singleTop? I'm just experiencing that and will probably need to hardcode something in launched activity - so let me know if someone found some workaround (working on Android 4.2.2 on Nexus 7).Dmz
D
258

This is fixed by changing the launch mode to singleTop:

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

There's a bug / feature (?) in Android, which immediately reports result (which has not been set yet) for Activity, declared as singleTask (despite the fact that the activity continues to run). If we change launchMode of the parent activity from singleTask to singleTop, everything works as expected - result is reported only after the activity is finished. While this behavior has certain explanation (only one singleTask activity can exist and there can happen multiple waiters for it), this is still a not logical restriction for me.

Devilmaycare answered 23/8, 2010 at 9:51 Comment(17)
It seems a bug! ^^ very weird behavior!Pelton
If activity has singleTask launch mode it not need to receive results from sub-activityes using onActivityResult. Sub-activityes just call finish() and then start main activity with data intent. In main activity you must override onNewIntent method and process received intent.Pesade
launchMode="singleInstance" also causes this behaviourTonguetied
Worth nothing I needed SingleTop on my parent activity, not just the childCloudlet
I can't believe this bug, even in Android L Preview! Wasted hours trying to figure out what was going on until I came across this. +1 for the great explanation.Planoconvex
The solution works but it is weird that adding FLAG_ACTIVITY_SINGLE_TOP to the intent did not produce the same effect.Dialyser
@Cloudlet this probably depends on your activity stack and OS version.Hernando
Just removed the launchMode singleTask from the Activity that has to provide result data and it worked. THXNowicki
It seems did this not work for me, I tried singleTop on parent activity but to no avail. I also set the intent flag into FLAG_ACTIVITY_SINGLE_TOP, though the request now shows the correct value but the result is always 0.Ashur
@NeonWarge what Android version do you have? The issue exposes itself differently on different versions. The best you can do is open the source code of Android itself (as I did) and find out the reason of your problem in that way.Hernando
I was doing min version = 8 and target SDK at 22. I fix the problem by circumventing that approach anyway. And most importantly, that silly mistake I made, I mixed request code with result code. They were on different param places. I didn't notice that.Ashur
thanks! I'm using SDK 22, and support v4 fragment, it happened!. you save a lot of my time!Porosity
it happens on Kitkat 4.4.4, not happening on Lolipop.Stoicism
The problem is the Activity I am launching is Searchable so I can't remove this flag otherwise it totally screws over the search processing. Back to square one without a solution.Temekatemerity
It works with all popular brand devices and all version. Saved my another day. Thank you very much.Detta
Didn't worj for me yet on Samsung running Kitkat.Coyle
Single instance not working for me, some Samsung Galaxy Tablet API 19. Single top worksLaurelaureano
D
21

I solved my problem after removing intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); before calling fragment.startActivityForResult(intent, 0);.

Dishrag answered 17/6, 2015 at 4:34 Comment(2)
Thank you! This solved my problem. Is there an explanation for this somewhere?Isacco
There is an explanation for this in the docs for the flag these days "This flag can not be used when the caller is requesting a result from the activity being launched". Well its not an explanation but at least a warning!Connor
P
5

I just removed all my custom "android:launchMode" from my Activity and everything worked like a charm. It is not a good idea change this when you don't know EXACTLY what Android is understanding... Android is a little tricky in this way.

Pelton answered 10/10, 2011 at 22:41 Comment(0)
R
1

This happened to me when the intent had the Intent.FLAG_RECEIVER_FOREGROUND flag set.

(Yes, that flag isn't activity-related, but I had it on all my intents as part of a shotgun solution to a different problem.)

Rayleigh answered 26/6, 2015 at 22:44 Comment(0)
T
-1

Again as in Mayra's comment, setResult() has nothing to do with your problem. for some reason, MyConfigure class finishes itself and when it happens PreferenceActivity just assumes that there might be a result from MyConfigure because that's how you wrote the code.

this also happens when you force back any activity thats started with startActivityForResult()...

So, I think it's better to focus on why your MyConfigure class is forcibly finished.

Timothy answered 29/7, 2010 at 5:42 Comment(1)
MyConfigure class does NOT finish, your guesses are wrong, sorry. If It did, there would be no questionHernando

© 2022 - 2024 — McMap. All rights reserved.