Home » Android » Android return data to previous activity

Android return data to previous activity

Posted by: admin June 15, 2020 Leave a comment

Questions:

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

How to&Answers:

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

Answer:

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

Answer:

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

}