How to Pass Data from 2Nd Activity to 1St Activity When Pressed Back? - Android

How to pass data from 2nd activity to 1st activity when pressed back? - android

Start Activity2 with startActivityForResult and use setResult method for sending data back from Activity2 to Activity1. In Activity1 you will need to override onActivityResult for updating TextView with EditText data from Activity2.

For example:

In Activity1, start Activity2 as:

Intent i = new Intent(this, Activity2.class);
startActivityForResult(i, 1);

In Activity2, use setResult for sending data back:

Intent intent = new Intent();
intent.putExtra("editTextValue", "value_here")
setResult(RESULT_OK, intent);
finish();

And in Activity1, receive data with onActivityResult:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
String strEditText = data.getStringExtra("editTextValue");
}
}
}

If you can, also use SharedPreferences for sharing data between Activities.

Android with Kotlin - pass data back to previous Activity

It's not getting called because:

  1. You are not setting the result
  2. You look like are creating a new activity instead of finishing the current one (?)

In your first activity:

private fun launchNewActivity() {
val requestCode = [yourCodeHere]
val intent = Intent(this, ProductDetailsActivity::class.java)
startActivityForResult(intent, requestCode)
}

In your Second activity

private fun passProductAsResult(product: Product) {
val data = Intent()
data.putExtra("product", product.toString());

setResult(Activity.RESULT_OK, data);
finish()
}

Back in your First Activity override the method onActivityResult

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if(resultCode != Activity.RESULT_OK) return
when(requestCode) {
[yourRequestCode] -> { yourTextView.text = data.getStringExtra("product"); }
// Other result codes
else -> {}
}
}

Make sure the data type is either Serializable or Parcelable if you're using complex data types.

How to pass data from 2nd activity to 1st (Popup) without press back button and close button?

Why not use a Button (perhaps with a drawable) instead of an EditText? This makes more sense because the user won't be typing anything into the EditText.

As for passing the data back and forth, just use startActivityForResult().

Before choosing template

After choosing template

How to pass data 1st activity to 2nd and 2nd activity to 3rd

The problem is in Here Activity2 code check it

You are passing item = null

Activity2 code:

case R.id.rlCompanyProfile:
Base item = null; // item object is null here
Company company = (Company) item ;// here you are passing null item object of your Company class
Intent intent = new Intent(this, AddCompanyActivity.class);
intent.putExtra("company", item);
intent.putExtra("edit", "editFrag");
startActivity(intent);
break;

EDIT

to send data to Activity use this

intent.putExtra("company", company);

To receive data in third activity use this

Company company=intent.getSerializableExtra("company");


Related Topics



Leave a reply



Submit