I am trying to create an AlertDialog welcome message and some other features related to the first run of the app on a device.
Check if application is on its first run I've seen this post, but I'm running into some problems. So when I set up my shared preferences and alert dialog like this: (Main Activity)
if(preferences.getBoolean("very_first_run", true)){
View view = MainActivity.this.getLayoutInflater().inflate(R.layout.welcome_message, null, false);
AlertDialog my_builder = new AlertDialog.Builder(this)
.setView(view)
.setPositiveButton("Get started", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
createAccountAlertDialog();
}
}).setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
preferences.edit().putBoolean("very_first_run", false).commit();
}
...and set it to null SharedPreferences preferences = null;
and then initialize it: preferences = getSharedPreferences("first_time", MODE_PRIVATE);
; when I try to pass it to another activity, their values never match up.
For instance, I'm retrieving the shared preferences like this in the AccountActivity:
SharedPreferences customizeActSharedPrefs = getSharedPreferences("first_time", MODE_PRIVATE);
boolean isUsersFirstTime = customizeActSharedPrefs.getBoolean("very_first_run", true);
...but when I log the shared preferences in the MainActivity, and in the AccountActivity, one shows true on first startup(MainActivity), whereas the other shows false(AccountActivity).
Hence, the MainActivity shared preferences is working, whereas the accountactivity shared preferences isn't.
My log looks like this:
2021-05-27 16:50:17.386 6620-6620/com.krish.mybank D/prefs: Main activitytrue
2021-05-27 16:50:21.199 6620-6620/com.krish.mybank D/prefs: Customize Activityfalse
What may be going wrong here?
Thanks!