Send data backwards to a previous activity

2019-04-21 08:17发布

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 selection 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.

4条回答
三岁会撩人
2楼-- · 2019-04-21 08:28

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.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-04-21 08:42
beautiful°
5楼-- · 2019-04-21 08:47

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

查看更多
登录 后发表回答