25

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.

Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
  • save the value in a global variable or use shared pref – Athul Harikumar Sep 06 '12 at 06:08
  • i have a problem with using global variable. if i am using it like this Activity_B.lat, Activity_A.lng. When i am in Activity_A, go to Activity_B and come back to Activity_A, close the app using home button, and reopen after 5 mins, i get null pointer exception since, the Activity_B which has this variable is destroyed – Archie.bpgc Sep 06 '12 at 06:10
  • U can refer this link... it will help u for sure...http://android-er.blogspot.in/2011/08/return-result-to-onactivityresult.html – Aditya Nikhade Sep 06 '12 at 06:11
  • you can use shared preference for it – Aamirkhan Sep 06 '12 at 06:17
  • have a look at that answer: [enter link description here][1] [1]: http://stackoverflow.com/a/15641238/2160507 – Pontios Jul 07 '14 at 16:02
  • Possible duplicate of [How to pass the values from one activity to previous activity](https://stackoverflow.com/questions/1124548/how-to-pass-the-values-from-one-activity-to-previous-activity) – Big McLargeHuge Oct 31 '18 at 19:39

4 Answers4

36

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.

Stefa168
  • 35
  • 2
  • 9
Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61
  • 1
    so i keep intent.putExtra("lat", lat); and then setResult(1, intent); then finish()?? – Archie.bpgc Sep 06 '12 at 06:16
  • and in onACtivityResult, data.getDoubleExtra("lat"); – Archie.bpgc Sep 06 '12 at 06:17
  • but 1 more thing. what if i press back button when i am in Activity_B. should i override onBackPress() in Activity_B and say resuldCode = 0?? is there a need or will android take care if nothing is sent in onActivityResult – Archie.bpgc Sep 06 '12 at 06:22
  • If you want to disable the back button, you can.. For this same reason, in activityB, check inside onActivityResult, whether you have got correct request and result code. So if user presses back button, he result code will be something different. – Shrikant Ballal Sep 06 '12 at 06:26
27

In Activity A:

// Add more, if you call different activities from Activity A
private static final REQUEST_GET_MAP_LOCATION = 0;

void doSomething() {
    ...
    startActivityForResult(theIntentYouUseToStartActivityB, REQUEST_GET_MAP_LOCATION);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_GET_MAP_LOCATION && resultCode == Activity.RESULT_OK) {
        int latitude = data.getIntExtra("latitude", 0);
        int longitude = data.getIntExtra("longitude", 0);
        // do something with B's return values
    }
}

In Activity B:

...
setResult(Activity.RESULT_OK, 
    new Intent().putExtra("latitude", latitude).putExtra("longitude", longitude));
finish();
...
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • i have a case where, Activity_A call Activity_B which inturn calls Activity_C and finish();... Now if i call finish() on Activity_C i get to Activity_A, but here in onActiviyResult, it is not entering the if block which you gave. thats expected because From Activity_B to Activity_C i am just using startActivity(intent). HOw to solve this case?? thank you – Archie.bpgc Sep 06 '12 at 07:22
  • @Archie.bpgc: When you call Activity_C in B, do you `setResult` before calling `finish`? – Heinzi Sep 06 '12 at 07:29
  • No i wasn't using anything except Intent(Activity_B, ACtivity_C), startActivity(intent) and finish(); – Archie.bpgc Sep 06 '12 at 07:33
  • but i then changed startActivityForResult(intent, 0); – Archie.bpgc Sep 06 '12 at 07:34
  • i thought no use in setResult in Activity_B because i dont have lat, lng values here, they are in Activity_C and moreover when coming3back from ACtivity_C to ACtivity_A i won't pass through ACtivity_B, since its finished – Archie.bpgc Sep 06 '12 at 07:35
  • @Archie.bpgc: If you get the values in Activity_C, Activity_B will have to wait for Activity_C to finish and then pass the values back to Activity_A. So, B need to `startActivityForResult` C and *not* finish yet. B can finish in `onActivityResult`, after putting the values from C back to its own result. – Heinzi Sep 06 '12 at 07:43
  • so i can startActivityForResult in B to go to C, then in C i setResult and finish(). Again in B in onActivityResult take those values and call finish() using these values in setResult. Then i will end up in A getting values from B, which actually are values from C?? am i right? – Archie.bpgc Sep 06 '12 at 07:52
  • @Archie.bpgc: Exactly, that's the plan. – Heinzi Sep 06 '12 at 08:03
10

Call your Activity_B with startActivityForResult(), from your Activity_A:

 //Starting a new Intent
 Intent nextScreen = new Intent(getApplicationContext(), Activity_B.class);
 // starting new activity
 startActivityForResult(nextScreen,1000);

Once you finished working on the Activity_B, you call setResult() to set the data, followed by finish() like this

 //Starting the previous Intent
 Intent previousScreen = new Intent(getApplicationContext(), Activity_A.class);
 //Sending the data to Activity_A 
 previousScreen.putExtra("Bla"," Blabla"); 
 setResult(1000, previousScreen);
 finish();

This will bring you back to your previous Activity_A.

In Activity_A, override onActivityResult().

 @Override  
  protected void onActivityResult(int requestCode, int resultCode, Intent data)     
  {
     super.onActivityResult(requestCode, resultCode, data);
     String bla = data.getStringExtra("Bla");       
  }

Found here

Community
  • 1
  • 1
Pontios
  • 2,377
  • 27
  • 32
1

use startActivityForResult to start B and setResult before B finish and handle onAcitivityResult in A

Sameer
  • 4,379
  • 1
  • 23
  • 23
logcat
  • 3,435
  • 1
  • 29
  • 44