开发者

Send data backwards to a previous activity

I would like some help in sending data backwards to an Activity that is already running.

I want the user to be able to select an option from a list and then that select开发者_开发问答ion be used in a previous activity. I know how to do it going forward using intents but can't see how that would work in this case without having an arbitrary number of the same activity windows running at the same time.

Sort of like changing the settings within the phone but more having access to a string. If you need any more information just ask.


There are two ways to do this, the first is rather than calling startActivity(), call startActivityForResult(), this is what the documentation has to say about it:

Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call the startActivityForResult(Intent, int) version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int, int, Intent) method.

http://developer.android.com/reference/android/app/Activity.html

The other way to do it is to send out a broadcast at the end of your activity, and register a broadcastReceiver() in your original activity.


If you start the second activity using startActivityForResult() rather than startActivity, then when the second activity completes you can set a resultCode and an Intent. It will then call the method:

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
}

in your first Activity, where you can read the resultCode and the Intent data


You need to use startActivityForResult.


Take a look at Activity#startActivityForResult() and Activity#setResult() methods

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜