Frank Bozzo
Frank Bozzo

Reputation: 121

Android: How to reset FirstRun SharedPreferences when my app is updated?

My app copies files over from res/raw to the sdcard on first run. I want it to update those files on every subsequent app update. How can i have it reset the firstrun preference to true on every app update?

Here is the relevant code:

/**
     * get if this is the first run
     *
     * @return returns true, if this is the first run
     */
        public boolean getFirstRun() {
        return mPrefs.getBoolean("firstRun", true);
     }

     /**
     * store the first run
     */
     public void setRunned() {
        SharedPreferences.Editor edit = mPrefs.edit();
        edit.putBoolean("firstRun", false);
        edit.commit();
     }

     SharedPreferences mPrefs;

     /**
     * setting up preferences storage
     */
     public void firstRunPreferences() {
        Context mContext = this.getApplicationContext();
        mPrefs = mContext.getSharedPreferences("myAppPrefs", 0); //0 = mode private. only this app can read these preferences
     }

     public void setStatus(String statustext) {
         SharedPreferences.Editor edit = mPrefs.edit();
         edit.putString("status", statustext);
         edit.commit();
      }

}

Upvotes: 12

Views: 5943

Answers (4)

Swami
Swami

Reputation: 163

Run this in the MainActivity's OnCreate

public void onUpdateFirstRun () {
    SharedPreferences prefs  = PreferenceManager.getDefaultSharedPreferences(this);
    PackageInfo pInfo;

    try {
        pInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);

        Log.d("VersionCode", pInfo.versionCode + " ");
        if (prefs.getLong(LAST_RUN_VERSION_CODE_KEY, 0) < pInfo.versionCode) {

            if (!isInitializedInSP(KEY)) {

                editor.putString(KEY, "");
            }

            //Finalize and Save
            editor.putLong(LAST_RUN_VERSION_CODE_KEY, pInfo.versionCode);
            editor.apply();

        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
}

Use method to check if you had already initialized it in previous version

public static boolean isInitializedInSP (String key) {
    SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContex());
    Object o = mPrefs.getAll().get(key);

    if (o != null) {
        return true;
    }
    else  {
        return false;
    }

}

Upvotes: 1

Jimmy Collazos
Jimmy Collazos

Reputation: 1924

I'm create class for this; download in https://gist.github.com/2509913

Example Use:

long versionInstalled = App.getVersionInstalled(this);
long current_v = App.getVersion(this);
if( versionInstalled  != current_v ){
    Log.w("TAG", "Veresion not valid"); 
}

Upvotes: 1

RivieraKid
RivieraKid

Reputation: 5961

In my app, I save in my shared preferences the version code of the app. At every startup, I check to see if the saved version code is lower than my current version code. If it is, I show a "what's new" dialog.

Give this code a whirl - I use it in my main activity's onCreate:

    PackageInfo pInfo;
    try {
        pInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);
        if ( prefs.getLong( "lastRunVersionCode", 0) < pInfo.versionCode ) {
            // TODO: Handle your first-run situation here

            Editor editor = prefs.edit();
            editor.putLong("lastRunVersionCode", pInfo.versionCode);
            editor.commit();
        }
    } catch (NameNotFoundException e) {
        // TODO Something pretty serious went wrong if you got here...
        e.printStackTrace();
    }

prefs is a private SharedPreferences object. This works if it's truly the first run, and for upgrades. At the end of the first-run code, the editor.putLong updates your shared preferences with the current version code of your app so the next run doesn't trigger your first-run code.

This also benefits from the fact that your version code must increase for the app to be seen as an upgrade by the market, so you don't need to remember to change a separate value to detect the first-run.

Upvotes: 22

Valentin Rocher
Valentin Rocher

Reputation: 11669

You could mimic what's done on the database side, with version numbers. Instead of having just a firstRun variable, have a couple with firstRun and versionNumber, and put a static version number field in your app, that you increment at each release. This way, you'll be able to check if the app has been updated, and do your operation on each update.

Upvotes: 3

Related Questions