Android return data to previous activity

2019-01-26 10:24发布

问题:

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

回答1:

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



回答2:

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



回答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();
                }
            });
    }

}