-
-
Notifications
You must be signed in to change notification settings - Fork 389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
inject SharedPreferences #4441
inject SharedPreferences #4441
Conversation
preferences = getSharedPreferences( | ||
getString(R.string.preferences_file_key), | ||
Context.MODE_PRIVATE | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why the SharedPreferences were created differently than everywhere else here?
@@ -125,7 +127,8 @@ private boolean activityTransitionWasRequested() { | |||
|
|||
@Override | |||
protected void attachBaseContext(Context newBase) { | |||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(newBase); | |||
// injected preferences not yet available in this point of the lifecycle | |||
SharedPreferences preferences = ((TuskyApplication)newBase.getApplicationContext()).sharedPreferences; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too hacky?
While I agree that it's better to inject preferences everywhere for consistency and to make testing easier, it's not true that a new instance gets created every time. The system keeps a static cache of SharedPreferences by name, and that cache includes the default SharedPreferences as well. |
@@ -125,7 +127,8 @@ private boolean activityTransitionWasRequested() { | |||
|
|||
@Override | |||
protected void attachBaseContext(Context newBase) { | |||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(newBase); | |||
// injected preferences not yet available at this point of the lifecycle | |||
SharedPreferences preferences = ((TuskyApplication)newBase.getApplicationContext()).getPreferences(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using a Dagger EntryPoint instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh that is nice, will do
@@ -99,11 +96,6 @@ class LoginActivity : BaseActivity() { | |||
.into(binding.loginLogo) | |||
} | |||
|
|||
preferences = getSharedPreferences( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This separate SharedPreferences instance was used to store temporary data that should ideally be stored to saveInstanceState instead of polluting the global SharedPreferences.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧐
Maybe it is not a new instance, but according to StrictMode it does the disk read every time and that is my main concern here |
I was surprised because I didn't notice that behavior in my apps before. It turns out that the cache is Context-related so the file is read once per Context. If we make sure to pass the Application Context for every retrieval like we do now, the file will only be read once and the |
a876d38
to
59be2fb
Compare
(this one is for @charlag)
Calling
PreferenceManager.getDefaultSharedPreferences()
will read the preference file from disk every time. This PR makesSharedPreferences
a singleton so they will only be created once at appstart (with a few exceptions where it is hard to inject, e.g. in theopenLink
helper) which should help getting our ANRs down.