Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i send back data using finish?

Tags:

Like i can send data from one activity to another using this:

intent.putExtra("Name", Value); 

how can i send data when i am using finish() to get back to the previous activity.

In my app from Activity_A i am going to Activity_B. In Activity_B i am marking a location on map, which gives me latitude and longitude. Then i want to use this lat and lng in Activity_A. But i don't want to get back to Activity_A using an intent, because i don't want to recreate the Activity_A since some data already filled will be lost.

like image 293
Archie.bpgc Avatar asked Sep 06 '12 06:09

Archie.bpgc


People also ask

How do I send data back to previous activity?

For example: In secondActivity if you want to send back data: Intent returnIntent = new Intent(); returnIntent. putExtra("result",result); setResult(Activity. RESULT_OK,returnIntent); finish();

How do you send data to activities?

We can send the data using putExtra() method from one activity and get the data from the second activity using getStringExtra() methods. Example: In this Example, one EditText is used to input the text. This text is sent to the second activity when the “Send” button is clicked.

How do I get back the result from child activity to parent in android?

Intent data = new Intent(); data. putExtra("myData1", "Data 1 value"); data. putExtra("myData2", "Data 2 value"); // Activity finished ok, return the data setResult(RESULT_OK, data); finish();

How do you set activity results?

The setResult method takes an int result value and an Intent that is passed back to the calling Activity. Intent resultIntent = new Intent(); // TODO Add extras or a data URI to this intent as appropriate. resultIntent. putExtra("some_key", "String data"); setResult(Activity.


1 Answers

As you are using intent.putExtra("Name", Value);, use the same thing while finishing the activity also.

For ex.:

From activityA you call activityB like:

intent.putExtra("Name", Value); now instead of startActivity() use `startActivityForResult()` 

And from activityB, while finishing the activity, call:

setResult(RESULT_OK); 

Now in activityA, your onActivityResult will be called, which is like:

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {     // TODO Auto-generated method stub } 

So inthis way you can handle it.

like image 72
Shrikant Ballal Avatar answered Sep 18 '22 15:09

Shrikant Ballal