Search code examples
androidandroid-activityoncreatestartactivityforresult

Activity calls a second Activity but result is sent back before the second Activity calls onCreate()?


I need some help here. Basically, I have an Activity. This uses the startActivityForResult() method to call a second Activity (which is part of the same app). The result code for this second Activity returns RESULT_CANCEL before the onCreate() method of the second Activity is called.

This is bewildering me. If I change the Intent and call the Android Messaging App Activity and not my own Activity I get the result code correctly after that Activity finishes.

It's pretty obvious to me that when you call your own Activity for a result you must do something different.

Testing on Android V2.2


Solution

  • Ok, so I have found the solution to this.

    If you have a launchMode of "singleTask" or "singleInstance" then you cannot receive a result from an Activity that you launch from your application. It will return immediately as RESULT_CANCEL. By changing the launchMode of the Activity to "standard" or "singleTop" this problem is solved.

    example:

    android:launchMode="standard" //This is the default so if you remove the 
                                  //attribute "android:launchMode=" from manifest 
                                  //file you should be ok as well.
    

    instead of

    android:launchMode="singleTask"
    

    I hope this helps out anyone who has the same problem.