I've 2 activities, Activity1 and Activity2.
In Activity1
I've a Button and TextView.
When the button is clicked Activity2 is started.
In Activity2
I've an EditText.
I want to display the data retrieved from EditText in Activity2 in the TextView in Activity1 when back is pressed from Activity2.
can someone help me with the code to make this work?
Start Activity2 with
startActivityForResult
and usesetResult
method for sending data back from Activity2 to Activity1. In Activity1 you will need to overrideonActivityResult
for updatingTextView
withEditText
data from Activity2.For example:
In Activity1, start Activity2 as:
In Activity2, use
setResult
for sending data back:And in Activity1, receive data with
onActivityResult
:If you can, also use SharedPreferences for sharing data between Activities.
From your FirstActivity call the SecondActivity using startActivityForResult() method.
For example:
In your SecondActivity set the data which you want to return back to FirstActivity. If you don't want to return back, don't set any.
For example: In secondActivity if you want to send back data:
If you don't want to return data:
Now in your FirstActivity class write following code for the onActivityResult() method.
Activity1 should start Activity2 with
startActivityForResult()
.Activity2 should use
setResult()
to send data back to Activity1.In Activity2,
In Activity1,
this is your first Activity1.
From here you are starting your Activity2.class by using startActivityForResult(mRequestCode, Activity2.class);
Now this is your second Activity, name is Activity2
Now when you done with your second Activity then you call setResult() method, from onBackPress() or from any button click when your Activity2 will destroy then your Activity1's call back method onActivityResult() will call from there you can get your data from intent..
Hope it will help to you...:)
Other answers were not working when I put
setResult
inonBackPressed
. Commenting call to superonBackPressed
and callingfinish
manually solves the problem:And in first activity:
and I Have an issue which I wanted to do this sending data type in a Soft Button which I'd made and the softKey which is the default in every Android Device, so I've done this, first I've made an
Intent
in my "A"Activity
:Then in my second Activity, I've declared a Field in my "B"
Activity
:then after I made my request successfully or whenever I wanted to tell the "A" Activity that this job is successfully done here change the value of resultCode to the same I've said in "A"
Activity
which in my case is "60" and then:PS: Remember to remove the
Super
from the onBackPressed Method if you want this to work properly.then I should call the
onActivityResult
Method in my "A" Activity as well:that's it, hope it helps you out. #HappyCoding;