简体   繁体   中英

Save game app after closing

I have this application game (android), and am just starting to learn more about android developpement (Using eclipse). So, when I test the game it's working pretty good. I have 18 levels, when I start the game, and for example I am in level 3, and I want to close it and get back to it later, It doesnt save that I am in level 3 and I must go all back from the beginning. I know that I should do some database to stock the data to load it after, but I do not know where to start with that.

Override onPause in the activity, and save it there.

Some of the other answers mentioned onDestroy , but that's not necessarily called if you just press eg the home button. onPause is the lifecycle method that's probably the best option when saving data, as it's called before the activity is destroyed, but it's still called.

You can use onDestroy too, but you'd have to call finish() when the app itself is minimized to make sure onDestroy is called. And in these cases, you'd most likely have to override onPause anyways.

As for the actual data saving, you can either use files, SQLite or shared prefs. There are other posts on Stack Overflow, but there's also the docs, that cover those topics.

Override onDestroy method to save the game whenever you close it. Use SQLite to save the data. You can find some tutorials on how to save that to a simple database.

Check out the Android Documentation for more information. There you will see which methods you will have to use. I think you will have to use the onDestroy().

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