Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Return to calling Activity

Newbie Question from an iPhone developer.

I have called the startActivity(intent) and the new activity loads. How do I go 'back' to the calling activity once a button is pushed. 'Popping' the activity off the stack basically.

like image 877
Ian Vink Avatar asked Jul 19 '10 00:07

Ian Vink


People also ask

How do I go back to previous activity on Android?

You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it'll take you back to the previous activity.

How do you send results back to activity?

Put the data that you want to send back to the previous activity into an Intent . The data is stored in the Intent using a key-value pair. Set the result to RESULT_OK and add the intent holding your data. Call finish() to close the Second Activity.

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();

What does finish () do in Android?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.


2 Answers

If you had created the new Activity with startActivity you just need to call finish. If you had spawned the new Activity by calling startActivityForResults then you need to call setResult and then finish in order to pass back data to the onActivityResult method of the prior Activity.

like image 132
Tim Kryger Avatar answered Oct 08 '22 15:10

Tim Kryger


Call finish() on your newly loaded activity. This is assuming you didn't call finish() on your previous activity (in which case you could always restart it).

like image 34
Andy Zhang Avatar answered Oct 08 '22 14:10

Andy Zhang