Zuuchq
Zuuchq

Reputation: 47

Shared Preferences Not Applying Changes

I have an activity (A) that checks my server for APK updates. When this function is called, whether there is or is not an update, I need the shared preferences to be edited so the application knows to skip or run the actual update activity (B). Question 1 is, why are the shared preferences not being edited by activity (A)? Question 2 is, if they are being edited, why isn't activity (B) reading them?

Thank You in advance!

Shared preferences should be edited here (Activity (A)):

    private void parseJson(String result) {
    try {

        JSONObject obj = new JSONObject(result);
        String updateMessage = obj.getString(Constants.APK_UPDATE_CONTENT);
        String apkUrl = obj.getString(Constants.APK_DOWNLOAD_URL);
        int apkCode = obj.getInt(Constants.APK_VERSION_CODE);

        int versionCode = AppUtils.getVersionCode(mContext);

        if (apkCode > versionCode) {
            if (mType == Constants.TYPE_NOTIFICATION) {
                showNotification(mContext, updateMessage, apkUrl);
            } else if (mType == Constants.TYPE_DIALOG) {
                SharedPreferences pref = mContext.getSharedPreferences("ActivityUpdatePREF", Context.MODE_PRIVATE);
                SharedPreferences.Editor ed = pref.edit();
                ed.putBoolean("activity_update", true);
                ed.apply();
                showDialog(mContext, updateMessage, apkUrl);
            }
        } else if (mShowProgressDialog) {
            SharedPreferences pref = mContext.getSharedPreferences("ActivityUpdatePREF", Context.MODE_PRIVATE);
            SharedPreferences.Editor ed = pref.edit();
            ed.putBoolean("activity_update", false);
            ed.apply();
            Toast.makeText(mContext, mContext.getString(R.string.android_auto_update_toast_no_new_update), Toast.LENGTH_SHORT).show();
        }

    } catch (JSONException e) {
        Log.e(Constants.TAG, "parse json error");
    }
}

Activity (B):

        SharedPreferences pref = getSharedPreferences("ActivityUpdatePREF", Context.MODE_PRIVATE);
    if(pref.getBoolean("activity_update", false)){
        Intent intent = new Intent(this, Main.class);
        deleteCache(getApplicationContext());
        startActivity(intent);
        finish();
    } else {
        functionUp();
    }

Upvotes: 0

Views: 488

Answers (3)

Swetabja Hazra
Swetabja Hazra

Reputation: 673

Use ed.commit() instead of ed.apply()

Upvotes: 0

KeLiuyue
KeLiuyue

Reputation: 8237

You code is ok,you should check you ed.apply(); is executed is your code .

In Activity(A) , you should check the order in which the program is executed .

1.Make sure that parseJson method is executed .

2.If you parse json error ,you should check JSONException.

Log.e(Constants.TAG, "parse json error");
e.printStackTrace();

3.If apkCode > versionCode is executed ,IsmType == Constants.TYPE_DIALOG executed ?

4.If apkCode < versionCode is executed ,Is mShowProgressDialog true in your code ?

And you can add Log to check it .

ed.apply();
Log.e(Constants.TAG, "SharedPreferences is apply");

Upvotes: 0

Robert I
Robert I

Reputation: 1527

Use commit() and not apply()

Unlike commit(), which writes its preferences out to persistent storage synchronously, apply() commits its changes to the in-memory SharedPreferences immediately but starts an asynchronous commit to disk and you won't be notified of any failures. If another editor on this SharedPreferences does a regular commit() while a apply() is still outstanding, the commit() will block until all async commits are completed as well as the commit itself.

https://developer.android.com/reference/android/content/SharedPreferences.Editor.html#apply()

Upvotes: 1

Related Questions