onActivityResult() called prematurely

AndroidAndroid Activity

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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

Solution 3 - Android

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.

Solution 4 - Android

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][1].)

[1]: https://code.google.com/p/android/issues/detail?id=104308 "Foreground service is killed as soon as an alarm broadcast is received after swiping away app in recent apps"

Solution 5 - Android

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.

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
QuestionEugene Mayevski &#39;CallbackView Question on Stackoverflow
Solution 1 - AndroidEugene Mayevski 'CallbackView Answer on Stackoverflow
Solution 2 - AndroidTPGView Answer on Stackoverflow
Solution 3 - AndroidFelipeView Answer on Stackoverflow
Solution 4 - AndroidSamView Answer on Stackoverflow
Solution 5 - AndroidoptimysteryView Answer on Stackoverflow