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.
How to pass the values from one activity to previous activity
To capture actions performed on one Activity within another requires three steps.
Launch the secondary Activity (your 'Edit Text' Activity) as a subactivity by using startActivityForResult
from your main Activity.
Intent i = new Intent(this,TextEntryActivity.class);
startActivityForResult(i, STATIC_INTEGER_VALUE);
Within the subactivity, rather than just closing the Activity when a user clicks the button, you need to create a new Intent and include the entered text value in its extras bundle. To pass it back to the parent call setResult
before calling finish
to close the secondary Activity.
Intent resultIntent = new Intent();
resultIntent.putExtra(PUBLIC_STATIC_STRING_IDENTIFIER, enteredTextValue);
setResult(Activity.RESULT_OK, resultIntent);
finish();
The final step is in the calling Activity: Override onActivityResult
to listen for callbacks from the text entry Activity. Get the extra from the returned Intent to get the text value you should be displaying.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (STATIC_INTEGER_VALUE) : {
if (resultCode == Activity.RESULT_OK) {
String newText = data.getStringExtra(PUBLIC_STATIC_STRING_IDENTIFIER);
// TODO Update your TextView.
}
break;
}
}
}
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 do I pass data between Activities in Android application?
The easiest way to do this would be to pass the session id to the signout activity in the Intent
you're using to start the activity:
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);
Access that intent on next activity:
String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");
The docs for Intents has more information (look at the section titled "Extras").
How to pass data from return activity to dialog in previous activity?
First you need to take the view object globally out of that checkIn
methoed
View view;
view = dialog.getCustomView();
final EditText etPassNo = (EditText) view.findViewById(R.id.etPassNo);
Now under your onActivityResult
method, as i can see you already getting the barcode there, so just initiate the EditText object again on that same simply set the data in it.
barCode = data.getStringExtra("barCode");
EditText etPassNo = (EditText) view.findViewById(R.id.etPassNo);
etPassNo.setText(barCode);
Let me know if that works.
How to pass a value from one Activity to another in Android?
You can use Bundle to do the same in Android
Create the intent:
Intent i = new Intent(this, ActivityTwo.class);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
String getrec=textView.getText().toString();
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString(“stuff”, getrec);
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);
Now in your second activity retrieve your data from the bundle:
//Get the bundle
Bundle bundle = getIntent().getExtras();
//Extract the data…
String stuff = bundle.getString(“stuff”);
Related Topics
Asynctask Won't Stop Even When the Activity Has Destroyed
Android: Taking Complete Control of Phone(Kiosk Mode), Is It Possible? How
How to Put a Border Around an Android Textview
How to Pass the Values from One Activity to Previous Activity
How to Add a Textview to a Linearlayout Dynamically in Android
How to Change the Android Actionbar Title and Icon
Difference Between Android.App.Fragment and Android.Support.V4.App.Fragment
Is There Any Way in Android to Get the Height of Virtual Keyboard of Device
Android Asynctask for Long Running Operations
How to Convert Image File Data in a Byte Array to a Bitmap
Android Sdk Manager Won't Open
How to Set Android Camera Orientation Properly
What Is an Android Pendingintent
How to Implement Multi-Select in Recyclerview
Show Gif File with Glide (Image Loading and Caching Library)
Android Locationclient Class Is Deprecated But Used in Documentation
How to Know an Application Is Installed from Google Play or Side-Load
How to Escape Special Characters Like ' in SQLite in Android