I am making an android app where data will need to be read in from a file and later saved back to that same file.
Basically, this is how my app will work:
MainActivity: User input determines which file to read from, then FieldActivity is started.
FieldActivity: Displays an object of type Data, which was read in from the file. The user can change the information stored in the Data object, so it needs to be saved back to the file when the app closes. However, this Data object is the same object as long as FieldActivity is running. (The user can navigate back to the MainActivity and choose a different file to read from, which will construct a new Data object.)
Should I save the data to the file in FieldActivity's onStop() or onPause() methods? I wasn't sure if there was a better way, seeing as how those methods are called every time you change between Portrait and Landscape. However, I can't think of any better option.
Edit: I could save it in onBackPressed(), but then if the user exits the by pressing the home button, it won't be saved.
I'm going to read the data from the file in a method that is called when the user clicks a button in the MainActivity, and then immediately start a FieldActivity. However, that Data needs to be the same object even between orientation changes in FieldActivity. I could store it in a static field, but I've heard that that is bad practice, and as such, I would assume there is a better way.
In summary: I need to read data from a file, and save it back to the file after the user makes changes. When in the application lifecycle is the best time to do this?
(I apologize in advance for anything I've done wrong, especially if this question has already been asked. I'm new to StackOverflow.)