SharedPreferences behaviour on Update/Uninstall

前端 未结 4 1367
不知归路
不知归路 2020-12-20 21:35

I am using shared preferences to store the number of times my application has been launched. Only on the first launch i display an Welcome message telling the user about the

4条回答
  •  再見小時候
    2020-12-20 21:53

    If you get don't set dontShowagin you will get false by default .So you want to show dialog and next time not.So just change the value in preference to true so that next time it works.Also you are incrementing the counter without actually incrementing it.Use +1 with the previous one.

    SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0);
                if (prefs.getBoolean("dontshowagain", false)) {
                    return;
                }
    
                SharedPreferences.Editor editor = prefs.edit();
    
                // Increment launch counter
    
                editor.putBoolean("dontShowagain",true);
                launch_count = prefs.getLong("launch_count", 0)+1;
                editor.putLong("launch_count", launch_count);
    

提交回复
热议问题