Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7896591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:56:01+00:00 2026-06-03T07:56:01+00:00

I am wondering what strategies others use to avoid data loss when saving files

  • 0

I am wondering what strategies others use to avoid data loss when saving files on Android. I have a couple of game apps that and essentially, they potentially save the game state/savegame whenever the user pauses (onPause).

This works in 99.99% of the cases, but every once in a while I receive an example of a savegame where the saving process has gone wrong. Usually, this is an ill-formed XML file, typically truncated at some arbitrary point. Based on the bug reports I receive, I believe that the problem mostly occurs when the user is interrupted during gameplay by a phone call or something like that and the Android OS then kills the app before it can finish saving. The code for saving the files is pretty simple, so I have difficulty seeing what else might cause this issue.

This is a serious problem, because it will usually result in the player’s save progress being ruined.

I was thinking of writing to an empty file first, and then copying to the “real” file only after, but I suspect that would simply increase the problem as it will overall take more time and still risks getting interrupted.

Anyone who has a secure way of doing this that Android is relatively guaranteed not to mess up?


So summarized, the options suggested so far (as I understand them):

  1. Use a service for the saving process, on the assumption that this will be less likely to be killed by the OS.
  2. Save in temp file; copy over when save is verified.
  3. Incremental saving of the game state (I actually already use this for player log information).
  4. Combination of 2 & 3.
  5. Move the saving to another thread, as the problem may be with ANR kills [DC’s comments below].

I do not think SharedPreferences will work for this kind of structured data, At the moment, neither of these methds seem like an ideal solution, so I am still open to suggestions.


I’ve not yet managed to test all of these approaches, so rather than waste the bounty, I have assigned it to the answer that I feel would be most likely to solve the issue. I plan to check through the various options still, though, prior to accepting an answer. Thanks for all of the good suggestions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:56:03+00:00Added an answer on June 3, 2026 at 7:56 am

    I think that saving little portions of data (not the whole game state) to the database every time user performs any action (not only when onPause() is called) would be the best approach in this case. However, this may require a lot of modifications to the code.

    The compromise would be to split the game state into smaller portions or sub-states (let’s say, round1, round2, …, players, etc) and again store data to appropriate files as user performs any action, not waiting for onPause() call. This would significantly reduce the probability of loss and at least would guarantee that the user doesn’t loose the whole game progress. Moreover, to avoid inconsistent states of your app, which may appear when the save process is interrupted, you’d better save data to temporary file first and only in case of success simply rename the file, not copying its content (let’s say, round1.xml.tmp rename to round1.xml).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering, and in need, of strategies that can be applied to reducing low-level
I'm wondering what strategies people use for reduced sign on with legacy applications and
I'm wondering what are some strategies that you've come up when dealing with this.
I have been looking at several deployment strategies for SSIS and wondering which one
I have a couple of strings that make up a CustomerInfo Object.I want to
I was wondering if people would share their best practices / strategies on handling
Wondering if there is any way to get the lambda expressions that result from
Wondering if anyone can suggest a good file replication tool that will replicate across
Wondering if there is a good way to generate temporary URLs that expire in
I have a package that will be inserting a single parent record in one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.