简体   繁体   中英

When to save the state of an app

I am building an Android game. Now, I am saving all changes of the user state (matches in SQLite, user's preferences in SharedPreferences, and other data in files) in the onDestroy() method of the main fragment (that fragment should be alive all the time and can open other activities, such as a the account fragment or playing fragment).

If I exit the app via Back button, everything works fine since onDestroy() of main fragment is called.

The problem is when I exit via Home button, or when main fragment is killed from OS. In these cases, onDestroy() isn't called, so the state of the game isn't saved.

I know that I can resolve that if I save the game in the onPause() instead of onDestroy() , since it is always called. But the storage operation, in my case, is very big (SQLite + SharedPreferences + files, a lot of data) and do that in onPause() means inefficiency.

Is there any other way to manage this?

Have you tried this solution - http://nisha113a5.blogspot.com/2012/01/intercept-home-key-android.html ?

Try to hook Home button pressing, and at this moment save user state.

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