0

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!

CodingChap
  • 1,088
  • 2
  • 10
  • 23
  • You are using different contexts (ie Activity). Use getApplicationContext() instead – Marcin Orlowski May 28 '21 at 00:19
  • use shared preference change listener to get updated value.@MarcinOrlowski – androidLearner May 28 '21 at 01:00
  • @MarcinOrlowski Thank you for your response. So basically I should just create it like preferences = getApplicationContext().getSharedPreferences("first_time", MODE_PRIVATE); – CodingChap May 28 '21 at 01:52
  • Hi, tried it, but it's not working. Also, the AlertDialog only shows up if I manually change the shared preferences name. For instance, if I run it on a completely different emulator, the shared preferences is still saved at false and the welcome alert dialog doesn't show up. – CodingChap May 28 '21 at 01:58

0 Answers0