1

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();
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
Gangadhar Nimballi
  • 1,534
  • 3
  • 18
  • 46

1 Answers1

1

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.

Community
  • 1
  • 1
Byron
  • 1,313
  • 10
  • 22