Android Best Way To Save Very Tiny Data On Device
in my application launching activity i need to check three things every time app is started App Version is User Login is User Account Created I am using Firebase for database
Solution 1:
This is the way I would do it using SharedPreferences
.
First create a separate class( I use it to save other informations like url, constants etc.) In that create a SharedPreferences
.
publicclassproject_constants {
privatestaticStringPREF_NAME = "project_pref";
privatestaticSharedPreferencesgetPrefs(Context context) {
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
publicstaticbooleangetUserLogin(Context context) {
returngetPrefs(context).getBoolean("login", false);
}
publicstaticvoidsetUserLogin(Context context, boolean input) {
SharedPreferences.Editor editor = getPrefs(context).edit();
editor.putBoolean("login", input);
editor.apply();
}
Now when the user log's in, you should use project_constants.setuserLogin(getApplicationContext,True);
.
Now when you want to check whether the user has logged in or not, you can use project_constants.getuserLogin(getApplicationContext);
, if that's true, the user is logged in, else no.
Solution 2:
For the first time you should save your data in SharedPreference when data is ready from firebase with:
SharedPreferencespref= getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
editor = pref.edit();
editor.putBoolean("userLoginCheck", false);
editor.commit();
Then you can get preference value for next times with:
booleanisLoogenIn= pref.getBoolean("userLoginCheck", true);
Post a Comment for "Android Best Way To Save Very Tiny Data On Device"