Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android return data to previous activity

I need you help: I want to putExtra data to the previous activity before finishing the current activity.

Eg: Activity A start Activity B When I finish Activity B I want in Activity A new data.

How I can do that? Many thanks before

like image 631
prcaen Avatar asked Nov 29 '12 14:11

prcaen


People also ask

How do I send data back to previous activity?

Android App Development for BeginnersStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 4 − Add the following code to res/layout/activity_second.

How do I go back to previous activity on Android?

Android activities are stored in the activity stack. Going back to a previous activity could mean two things. 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 can I transfer value from one activity to another in Android?

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 do you set activity results?

The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.


2 Answers

Android SDK explanation here, better SO question answer+example here.

like image 135
NickL Avatar answered Oct 10 '22 02:10

NickL


Use startActivityforResult to open the activity B..then override onActivityResult(int, int, Intent) in your activity A..

Example:

public class MyActivity extends Activity {
 ...

 static final int PICK_CONTACT_REQUEST = 0;

 protected boolean onKeyDown(int keyCode, KeyEvent event) {
     if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
         // When the user center presses, let them pick a contact.
         startActivityForResult(
             new Intent(Intent.ACTION_PICK,
             new Uri("content://contacts")),
             PICK_CONTACT_REQUEST);
        return true;
     }
     return false;
 }

 protected void onActivityResult(int requestCode, int resultCode,
         Intent data) {
     if (requestCode == PICK_CONTACT_REQUEST) {
         if (resultCode == RESULT_OK) {
             // A contact was picked.  Here we will just display it
             // to the user.
             startActivity(new Intent(Intent.ACTION_VIEW, data));
         }
     }
 }
}

check http://developer.android.com/reference/android/app/Activity.html

like image 44
Nermeen Avatar answered Oct 10 '22 01:10

Nermeen