10

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

prcaen
  • 2,429
  • 4
  • 25
  • 32

3 Answers3

9

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

Community
  • 1
  • 1
NickL
  • 4,258
  • 2
  • 21
  • 38
7

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

Nermeen
  • 15,883
  • 5
  • 59
  • 72
3

Use startActivityforResult to start Activity B. Implement override onActivityResult(int, int, Intent) method in Activity A and setResult in ActivityB.

Example:

public class ActivityA extends Activity {

static final int REQUEST_CODE = 0;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.xyz);

        DateBtn.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    startActivityForResult(new Intent(ActivityA.this,ActivityB.class), REQUEST_CODE);
                }
            });
    }

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


public class ActivityB extends Activity {

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.xyz);

        BackBtn.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
            Intent intent = new Intent();
            intent.putExtra("DATA", "your string");
            setResult(RESULT_OK, intent);
            finish();
                }
            });
    }

}
Ahsan Kamal
  • 1,085
  • 2
  • 13
  • 34
  • Use startActivityforResult in Activity A to launch activity B and use @override onActivityResult(int, int, Intent) method in your activity A to get data from B Activity. – Ahsan Kamal Jan 13 '16 at 12:37