Android returns data to previous activity

I need you to help: I ​​want to transfer data to a previous activity before ending the current activity.

For example: Activity A start Activity B When I complete Activity B, I want new data in Activity A.

How can i do this? Thank you very much before

+6
source share
3 answers

The description of the Android SDK is here , it is better to answer the question ++ here .

+7
source

Use startActivityforResult to open action 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 out http://developer.android.com/reference/android/app/Activity.html

+6
source

Use startActivityforResult to start Activity B. Add an override of the onActivityResult (int, int, Intent) method to Activity A and setResult to 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(); } }); } } 
0
source

All Articles