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:
- You are not setting the result
- 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().
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
How to Change Spinner Text Size and Text Color
Android.Content.Activitynotfoundexception:
Runtimeexception: Your Content Must Have a Listview Whose Id Attribute Is 'Android.R.Id.List'
How to Get Pixel Color in Android
Android - Cancel Asynctask Forcefully
How to Include *.So Library in Android Studio
How to Align Views at the Bottom of the Screen
Perform a Task on Uninstall in Android
How to Create a Closed (Circular) Listview
Error "The Connection to Adb Is Down, and a Severe Error Has Occurred."
Android: Progressdialog.Show() Crashes with Getapplicationcontext
List of Android Permissions Normal Permissions and Dangerous Permissions in API 23
Lock the Android Device Programmatically
How to Change Datepicker Dialog Color for Android 5.0
Singleton Object Becomes Null After App Is Resumed
How to Detect Swipe Direction Between Left/Right and Up/Down