5

I'm launching an activity using startActivityForResult() from a button handler, and my onActivityResult() is being called instantly, even before the onCreate() for the target activity is being hit.

public void onGraphNext (View target) {
    Intent i = new Intent(this, AddCommentActivity.class);
    startActivityForResult(i,6);    // 6 is arbitrary request code               
}    

. . .

protected void onActivityResult(int requestCode, int resultCode,
        Intent returnData) {
   if  ( (resultCode == RESULT_OK) && (requestCode == 6) ) {
   Bundle extras = returnData.getExtras();
   comment = extras.getString("comment");
     }
} 

The result code returned is 0 and the request code is 6. Elsewhere on StackOverflow I've seen people report this problem and the solution was to not use singeInstance for the launchMode in the manifest. But I'm using standard . . .

<activity android:name="AddCommentActivity"
          android:configChanges="orientation"
          android:screenOrientation="portrait"
          android:launchMode="standard"></activity>  

Thanks in advance for any insights!

EDIT: I made a simple test program and I can reproduce the problem reliably when the caller ("launcher") - the activity with the onActivityResult - is a singleInstance and the Activity being invoked ("launchee") is standard. i.e.,

<activity android:name="Launcher"
          android:screenOrientation="portrait"
          android:launchMode="singleInstance"></activity> 

<activity android:name="Launchee"
          android:screenOrientation="portrait"
          android:launchMode="standard"></activity>  

This is a problem for me because in the real app, the called must be a singleInstance for other reasons, but it wants to have buttons to start up other activities to request user input. How else to do this if I can't use startActivityForResult?

user316117
  • 7,971
  • 20
  • 83
  • 158
  • I think this is what you are looking for : http://stackoverflow.com/questions/3354955/onactivityresult-called-prematurely?rq=1 – Ashwini Bhangi Mar 28 '13 at 16:49
  • @Ashwini Bhang - the marked answer in the link you referenced was about avoiding singleTask, and other similar questions on S.O. referred to singleInstance as having the same issue, but as I explained, I'm not using any of those launch modes. Your link had no reference to any such problem with the standard launchMode - could you clarify what you saw that you thought might help? – user316117 Mar 28 '13 at 16:58
  • what is the orientation of the activity that call startActivityForResult – Hoan Nguyen Mar 28 '13 at 19:52
  • 1
    remove android:configChanges="orientation", this activity is always in portrait mode. – Hoan Nguyen Mar 28 '13 at 19:54
  • @Hoan Nguyan: that was a nice sighting so I'll give you a +1 on it, but it has nothing to do with the problem in question. I changed it and onActivityResult still gets called prematurely. – user316117 Mar 28 '13 at 20:09
  • Try removing `android:launchMode="standard"` Since you are setting it to default it is not necessary to set it. Granted I don't see a way that doing so would cause an error, but never hurts to try. – FoamyGuy Mar 28 '13 at 20:11
  • I tried that - no change. As you say, it's the default anyway. This is why I started that other thread - http://stackoverflow.com/questions/15690796/tools-for-tracing-calls-to-onactivityresult#15690796 I want to see if there's a way to escape the try-this-try-that approach and figure out what's actually happening. There are questions about onActivityResult either getting called too soon or not being called at all ALL OVER the web but I've never seen a systematic technical description of when or how it gets called anywhere! So we all end up guessing. – user316117 Mar 28 '13 at 21:49
  • I've update my post with a new edit describing some further discoveries. – user316117 Mar 28 '13 at 22:23

1 Answers1

15

You cannot use startActivityForResult() if the activity being started is not running in the same task as the activity that starts it. This means that neither of the activities can have launchMode="singleInstance". If you require that these 2 activities run in different tasks, then you need to use a different mechanism. The documentation for startActivityForResult() sorta-kinda hints at this:

"For example, if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result."

What you can do is to simply start the activity using startActivity() and have the called activity call startActivity() to return to your activity, sending back data as necessary as extras in the Intent it uses.

However, you might consider whether you really need these special launchModes. In general they are only necessary for HOME-screen replacements and other very special applications. Most developers who use singleInstance or singleTask launch modes are using them incorrectly.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 6
    I've discovered that the behavior varies across API levels. In Lollipop on my Nexus 5, startActivityForResult() worked fine when I was launching a singleInstance activity, however, on a Galaxy Note II running 4.1.2, onActivityResult() is called immediately. – rrbrambley Feb 20 '15 at 18:24
  • @rrbrambley That's hard to believe. Are you sure your `singleInstance` activity is being launched in a separate task? If you don't set the `taskAffinity` in the manifest it won't be launched in a separate task and isn't really being launched as `singleInstance`. – David Wasser Feb 23 '15 at 10:44
  • Me too. In Lollipop on my Lenovo K3 Note, startActivityForResult() worked fine. however, on a Galaxy Note II running 4.4.2, onActivityResult() is called immediately. Don't know how to fix these problem. – Boontawee Home Oct 16 '15 at 10:13
  • @BoontaweeHome please open a new question if you are having problems. Adding such comments to existing questions/answers isn't helpful. – David Wasser Oct 16 '15 at 11:10