1

I really have to use another thread to read and write a sharedPreferences file?

I'm storing an integer array in sharedPreferences file, I convert it in string to store there, ok, this array corresponds to the itens of a gridView, then I have to read it on onCreate() and write it on onDestroy(), the problem is I'm calling an asyncTask on onCreate() and this is causing a lot of problems... the main one is that the onCreate() is been called many times (every time that other activity came in front mine, it is destroyed and restarted...), I found out in here that a kind bug occurs when other thead is called on onCreate(), then I had to save some things on onSaveInstanceState(), but this only resolved my problem in part. I already tested do not use other thread and read and write the file directly on UI thread, and this worked nicely, but I always heard I have to use other thread to any kind of I/O... then I'm in doubt...

After all, I can or I can't read and write a sharedPreferences file without call other thread?

Thx

Cristina
  • 387
  • 1
  • 18
  • You can read/write to SharedPreferences without problems in UI thread if its small. It's all cached and done async in background. Check this answer http://stackoverflow.com/a/4371883/1626594 for a clearer explanation. –  May 21 '17 at 14:53
  • Really thx @Francisco M. – Cristina Jun 01 '17 at 13:04

0 Answers0