0

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!

Louis Bouchard
  • 199
  • 3
  • 9

2 Answers2

0

According to Save data in activity's onDestroy method You should save your data in the activity's (activity 2 in your case) onStop method. Technically speaking, you could then save that wherever you wanted and pass it back to activity 2 in the onStartActivity intent.

miversen33
  • 573
  • 5
  • 19
  • I searched how to do that and I couldn't do it. because when i first open the activity 2 there are no editText so nor extras saved so it does an error.. – Louis Bouchard Aug 05 '17 at 19:31
  • So save the information as a string variable and when you create the edit texts, use that string to fill them? – miversen33 Aug 05 '17 at 19:32
0

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

Bob
  • 13,447
  • 7
  • 35
  • 45
  • I don't want it to be saved when the app is closed, only when they back off to the menu (activity 1) and come back in the activity 2. How can i achieve that? I searched for singleton class but I didn't find a lot of info.. Thanks. – Louis Bouchard Aug 05 '17 at 19:04
  • When you press back, the Activity2 gets destroyed and along with that, all the EditText values are destroyed. So you need some place to store the values, say in a DataStore class, and once the user goes back to the Activity2, you have to retain the values from the DataStore and set it to the EditText of the Activity2. If you need more simpler solution, have a bunch of static Strings in the Activity2. And save the EditText values in those Strings. Once the user goes back to Activity2, you can set those Strings to EditTexts. – Bob Aug 05 '17 at 19:10
  • omg it's so simple.. I'm sorry for this dumb question! Thank you very much! – Louis Bouchard Aug 05 '17 at 19:52