Android - Shared Preferences are lost sometimes

前端 未结 5 1504
北海茫月
北海茫月 2020-12-08 15:12

Some of the users of my application complain that sometimes (in a random way) the settings of my application are getting reverted to their default state (usually after a reb

5条回答
  •  半阙折子戏
    2020-12-08 15:43

    According to the SharedPreferences.Editor documentation:

    Note that when two editors are modifying preferences at the same time, the last one to call commit wins.

    From this I gather that multiple simultaneous commits will not wipe out your preferences, but it's possible that not all changes you are attempting to write will end up getting written if multiple Editor instances are being used simultaneously. To avoid this you could put all preference modifications in synchronized blocks or even use one synchronized static method for all preference writing.

提交回复
热议问题