简体   繁体   中英

When should I read/save data in a file in an Android app?

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.)

Hello and welcome to Stack Overflow:). umm... from what i understood you get information from edittext and want to store it back to the file only before you leave the activity...if thats true and assuming you use the back press button to go back you can store everything on overriding the onBackPressed method...

but it all depends on when you want to store everything:

onPause() - is called when app is partly obscured but still visible and still running in FG (like when you open a dialog). onStop() - is called when app is no longer visible and is entirely in BG (suck as going back to different activity (if you didnt kill it)). I hope you are familiar with the activity lifecycle so... it all depends on when do you want to store your new data. i would say before you leave the activity...so just before you call finish()...this is usually when the user is done setting whatever he wants...so i would save onBackPressed... onStop is also a valid solution... Im not sure on how you planned your activity flow... but for me... onBackPressed or onStop...onPause.. wont help you here...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM