etherton
etherton

Reputation: 942

OnActivityResult getting called twice in Android

I used this code to launch an activity:

 Intent intent = new Intent(this, OneTimeActivity.class);
 intent.putExtra(Constants.HOST_KEY, host);
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
 startActivityForResult(intent, 1000);

And for some reason my OnActivityResult() is called twice. Once when I first call startActivityForResult and once when the result actually ends. What's also weird is that the Intent data is always null, and the resultCode is always 0

Why is this happening? Shouldn't I only get a callback from OnActivityResult() once and also once? Shouldn't I get the result code I specify in setResult()?

Upvotes: 1

Views: 3022

Answers (2)

Chintan Shah
Chintan Shah

Reputation: 1764

In addition to etherton's answer, also check whether your activity is declared as singleTask or not.

Open AndroidManifest.xml and check for android:launchMode="singleTask" for your called activity. Remove this line and it will work as expected.

Note: If you really want your activity as singleTask then you have to handle your callbacks on your own.

Upvotes: 0

etherton
etherton

Reputation: 942

Well after about 2 hours of debugging it seems

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

Was the problem. Once I removed that everything worked as expected, which I guess makes sense. I can't clear the back stack and expect results to get returned correctly to the back stack I just cleared.

Upvotes: 3

Related Questions