I have two Activities, Activity 1 has 2 EditText, it takes input and goes to the next activity. Now if I go back to the previous activity and enter different input in EditText then it doesn’t get updated in the next activity. It still takes the earlier input.
Question posted in Android Studio
The official documentation can be found here.
The official documentation can be found here.
2
Answers
you have multi ways that you can do:
when you back to the first activity, finish the second activity. and open it again.
update your UI with new data, in the
onResume
method of the second activity. you can store data in a singleton class.The first answer is useful. But if you want to change the data live when the user types anything, then you can follow this example:
Make the public interface
Implement it in SecondActivity
Override the interface method, then change the text value in it.
Then initialize & call the interface method from FirstActivity when user changes the edittext text like this :
In
onCreate()
And that’s it!
NOTE : You must check if the interface is not null as the first time you run the app, the second activity won’t get created first, so it can create
nulPointerException
when initializing the interface, then check if the edittext value is not null & then get Text from edittext and pass it into interface.So the data will be updated immediately when user types any value in edittext.
If you want to save the data to SecondActivity when user presses the save button, then you can go with the First answer. It’s also right 🙂