2

In my app, I start at the main activity, and there is a plus sign that takes you through a series of popup windows. Inside the popup windows, it has edittexts that the user can edit in order to set the title of a button. The newly created button is made when another button is pressed. The newly created button is then put on the MainActivity and all popup windows are dismissed.

My question is, how would you save that newly created button when the app is completely terminated (when you swipe right to close the app in the overview window)? I want to be able when I reopen the app, the button is still there, and the app doesn't return to its original state.

Thank you in advance. Hopefully this post makes sense.

How do you save the current state of the mainActivity and recreate it on reopen.

user117
  • 21
  • 3
  • 1
    Does this answer your question? [How to save an activity state using save instance state?](https://stackoverflow.com/questions/151777/how-to-save-an-activity-state-using-save-instance-state) – fjsv Jun 17 '20 at 15:43
  • That post helped a lot, but how would I save a button in shared preferences if that is possible? – user117 Jun 17 '20 at 15:50

1 Answers1

1

For this kind of persistence I recommend using Shared Preferences. This will save specific information locally on the drive (Note: Not used for user interaction, only for your apps business logic, in general).

I suggest you save a unique Integer for every state your app is in and upon app startup, you simply read the Integer from the Shared Preferences and recreate the state manually.

Saving the whole apps state, like pausing your code, is highly complex, if not impossible on Android.

René Jörg Spies
  • 1,544
  • 1
  • 10
  • 29
  • Thank you for answering. How would I set up an integer system like you suggested? Do you have a link to a tutorial that could help me? Sorry, I am fairly new to all of this. – user117 Jun 17 '20 at 15:56
  • There is a link to the official Google guide to `Shared Preferences` provided when you click on the first occurrence of "`Shared Preferences`" in the question above. – René Jörg Spies Jun 17 '20 at 15:57
  • Whoops sorry about that, did not realize it was linked. Thank you for your help. – user117 Jun 17 '20 at 15:59