Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 679
SamRowley Avatar asked Jan 04 '11 14:01

SamRowley


People also ask

How do I send data back to previous activity?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

How would you send data back to a previous activity in Java?

Start Activity2 with startActivityForResult and use setResult method for sending data back from Activity2 to Activity1. In Activity1 you will need to override onActivityResult for updating TextView with EditText data from Activity2. If you can, also use SharedPreferences for sharing data between Activities.

How do you send data from first activity to third activity?

You can pass the value in 2 ways: Either you make a global Class and set the value in that class and access that class in your 3rd activity. You can use Intent to send your values from 1st activity to 2nd activity.

How does kotlin send data to previous activity?

As there are many methods to send the data, but in this article, we will use startActivityForResult() method. Here we need to launch a child activity using startActivityForResult() method. Then from child activity, we can easily send data back to Main Activity.


2 Answers

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.

like image 114
Leif Andersen Avatar answered Jan 26 '23 11:01

Leif Andersen


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

like image 36
dave.c Avatar answered Jan 26 '23 12:01

dave.c