Search code examples
androidandroid-activityandroid-edittextsaveandroid-ondestroy

AndroidStudio - Save EditText when activity change


Ok so, there's a list of editText that the user fill with all names of players, say it's activity 2. And then click on start and play the game on activity 3. When he comes back to activity 2, the names are still in the editTexts and he can just add a participant if he wants and that's okay, but when he backs off in the menu, activity 1, and then open the activity 2 again, all the names are erased.

Is there a way to save the editText content in their places even when he leaves the activity 2 and comes back in it after? How can you achieve that?

Can you avoid calling the destroy method when back is pressed or do you need to save them somehow and put them back in?

Thanks!


Solution

  • If you want the names only during the application is alive, you can store the names in a Singleton class.

    Or if you want the names even if the application is killed and opened again, you have to store it in a persistent storage. Either SharedPreferences or a Database (SQLite).

    Check out the docs:

    https://developer.android.com/training/basics/data-storage/shared-preferences.html

    https://developer.android.com/training/basics/data-storage/databases.html