Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharedPreferences.getBoolean returns true everytime

I made a class for handling important data changes such as App Purchase Status and other stuff .

For this goal I have created a class which does the setting and reading of the values. but the problem is whenever I call the appIsPurchased() method, the result is true while it hasen't been changed since app installation and its first initial launch.

This is my code:

/**
 * Created by neemasa on 5/29/14.
 * This class handles more crucial data values within app.
 */
public class AppCore {

    private SharedPreferences settings;
    private String keyPurchase = "app_purchased";
    private Context context;

    public AppCore(Context context){
        this.context = context;
        settings = PreferenceManager.getDefaultSharedPreferences(context);
    }

    public void setAppInPurchasedMode(String status){
        if (status.equals("successful")){
            settings.edit().putBoolean(keyPurchase, true).commit();
        }else if (status.equals("failed")){
            settings.edit().putBoolean(keyPurchase, false).commit();
        }

    }
    public boolean appIsPurchased(){
        boolean purchased = false;
        if (settings.getBoolean(keyPurchase,true)){
            purchased = true;
        }
        return purchased;
    }

}


Question 1st: is there something wrong with my code? if there is then why appIsPurchased() always return true?
Question 2nd: do all values in the shared preferences are true by default?

Meanwhile when I use this class in my code the toast "Purchased!" runs even when app is running for the first time.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AppCore appCore = new AppCore(getApplicationContext());
        if (appCore.appIsPurchased()){
            Toast.makeText(getApplicationContext(),"Purchased!",Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(getApplicationContext(),"Not Purchased!",Toast.LENGTH_SHORT).show();
        }
}
like image 287
Nima Sakhtemani Avatar asked May 29 '14 13:05

Nima Sakhtemani


People also ask

Are SharedPreferences persistent?

Android's built-in SharedPreferences storage mechanism allows us to store information that persists throughout the entire app.

Is SQLite better than SharedPreferences?

To give an example, SharedPreferences are useful for storing user preferences, where there are just a handful of variables that need storing. SQLite on the other hand would be better for storing data where there is a large set of items, such as song titles in a music library which need to be searched through.

Can we store large amount of data in SharedPreferences?

SharedPreferences are not intended to store a lot of data, there is no limit per se (since it is an xml file), but for larger sets of data, I would suggest using Room (or SQLite for the older projects). There is also another reason why storing in a database makes more sense.

How can I check my data usage on SharedPreferences?

Click on the package name for your application. After that click on the shared_prefs folder and inside that open the shared_prefs. xml file. Now you will get to see the data which we stored in our shared preferences from our application.


2 Answers

Actually there is a problem in your code!! thats why its always showing purchased!!

 if (settings.getBoolean(keyPurchase,true)){
            purchased = true;
        }

in this lines if the keyPurchased tag if not used , u are passing true value by default so when u call

if (appCore.appIsPurchased()){

it always return a true value.. The solution is that make sure that the preference values are set before u call them.. hope this helps

like image 135
George Thomas Avatar answered Sep 25 '22 02:09

George Thomas


Found It, the problem is that I was thinking

settings.getBoolean(keyPurchase,false) 

returns the value of keyPurchased variable but the fact is it only returns the variable itself not its value so I fixed the problem by changing the method of my class to this:

public boolean appIsPurchased(){
    return settings.getBoolean(keyPurchase,false);
}
like image 35
Nima Sakhtemani Avatar answered Sep 23 '22 02:09

Nima Sakhtemani