8

What is use of start activity for result in Android? Please give example and what is difference between startactivity and startactivityforresult?

Cœur
  • 37,241
  • 25
  • 195
  • 267
mohan
  • 13,035
  • 29
  • 108
  • 178

2 Answers2

3

By calling startActivityForResult with Activity2, your current activity will be notified when the Activity2 is finished (back button pressed), and this way you can also get information from it.
This notification you can catch by overriding your activity's onActivityResult method.

This article about Android startActivity and startActivityForResult might be worth to look at.

rekaszeru
  • 19,130
  • 7
  • 59
  • 73
2

startActivityForResult() allows you to start activity and get some data back. Imagine that you have some file picker activity. You can start it and when user chooses the file, the result is given back to the original activity.

Also, it can be used if you simply want to ensure that the second activity has successfully done somethings.

The result code is obtained in onActivityResult method:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Result OK.d.
        if (requestCode == resultCode) {
            // do something good
        }
    }
dymmeh
  • 22,247
  • 5
  • 53
  • 60
Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • 1
    Surely the requestCode returned is the same value echoed back from the one passed in when you invoked startActivityForResult. The resultCode is usually something like RESULT_OK or RESULT_CANCELED. I don't see why you would test them for equality? – NickT May 03 '11 at 12:09
  • yeah that makes no sense. I like to keep constants of requestCodes that I use to spawn the intent at startActivityForResult and then to check them in a switch block in onActivityResult. – Ian May 03 '11 at 14:12