This is the navigation of my application:
Activity1
callsActivity2
→Activity2.finish()
, callActivity3
→Activity3.finish()
When Activity3
finishes, it calls the onResume
method of Activity1
. Now how can I pass a value from Activity3
to Activity1
?
putExtra() : This method sends the data to another activity and in parameter, we have to pass key-value pair. Add the below code in onClick() method. intent. putExtra("full_name", fullName);
You need put them in Extras (putExtras) and then pass from the current activity to the other one. You need capture your EditText value as String and then putExtra with Key - one each for your need and then retrieve them in the second activity.
Intent supports three ways to pass data: Direct: put our data into intents directly. Bundle: create a bundle and set the data here. Parcelable: It is a way of “serializing” our object.
Umesh shows a good technique but I think you want the opposite direction.
Step 1
When starting Activity 2 and 3, use startActivityForResult
. This allows you handle the result in the calling activity.
startActivityForResult(MY_REQUEST_ID);
Step 2
In Activities 2 and 3, call setResult(int, Intent)
to return a value:
Intent resultData = new Intent(); resultData.putExtra("valueName", "valueData"); setResult(Activity.RESULT_OK, resultData); finish();
Step 3
In your calling activty, implement onActivityResult
and get the data:
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == MY_REQUEST_ID) { if (resultCode == RESULT_OK) { String myValue = data.getStringExtra("valueName"); // use 'myValue' return value here } } }
Edit:
Technique #2
Yes, you can also use global application state by adding a class to your application that extends Application
, see this StackOverflow answer
Use 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)
See this tutorial.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With