Gangadhar Nimballi
Gangadhar Nimballi

Reputation: 1534

Why shared preferences has been reset up on app crash or force close Android

I have taken CSIPSIMPLE code and repackaged to com.mycompany.appname

The problem is, whenever app is crashed, All values are deleted from Shared preferences.

Why?

My Application is

public class BeemApplication extends Application {

    static BeemApplication application = null;

    public static SharedPreferences mPref;
    public static Editor mEditor;

    public BeemApplication() {

    }

    public static BeemApplication getInstance() {
        if(application != null) {
            return application;
        } else {
            return new BeemApplication();
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        mPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        mEditor = mPref.edit();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

}

In Activity I will get them like ,

BeemApplication.mEditor.putString(ctx.getString(R.string.pref_online_number), number).commit();

BeemApplication.mPref.getString(ctx.getString(R.string.pref_online_number), number).commit();

Upvotes: 1

Views: 3501

Answers (1)

Byron
Byron

Reputation: 1313

This is common issue that many have faced including myself. Have a look at this post Android - Shared Preferences are lost sometimes which shares your findings.

I would recommend not storing persistent data across shared preferences and rather use something like a database table to store settings.

Upvotes: 1

Related Questions