1

I have like 21 fragments in my production app and all of them either writing or reading shared preferences on UI thread. As I am currently trying to optimize app start-up time. I wonder accessing shared preference directly in fragment onCreate(), onResume() or as part of fragment property variables can add any cost to app start up time?

  • 1
    I don't think so. Accessing `SharedPreferences` is a fairly trivial task. While writing to it, Android provides a `.apply()` method to offload I/O to async. Reading however is perhaps negligible due to high performance storage drives. Happy to be proved otherwise though. – Clinkz Feb 28 '22 at 05:52
  • If you wanted the real answer short of what we _think_ it is, you can use the benchmark library and measure start up times. Now, I agree it's not a significant performance, but it all adds up, and it's absolutely not "instantaneous" nor zero. Preferences are stored in physical media, so I/O is involved, that's a lot more CPU cycles than reading an in-memory map... that being said, I'm sure the problem is elsewhere... you'd have to read/write a LOT of I/O to take a noticeable hit in perceived UI responsiveness, as well as energy consumption. – Martin Marconcini Feb 28 '22 at 10:25

1 Answers1

0

Have a look here and here and please read the differences between .apply() and .commit() Using the correct ones should have no visible impact on your app start-up time.

Marius Razvan Varvarei
  • 1,331
  • 1
  • 17
  • 21