Search code examples
androidsharedpreferences

SharedPreferences putInt method not working


private boolean rightReviewTiming() {
    int insertKitCnt = sharedPreferences.getInt("insert_kit_cnt",0);
    insertKitCnt++;
    sharedPreferences.edit().putInt("insert_kit_cnt", insertKitCnt);
    sharedPreferences.edit().commit();
    insertKitCnt = sharedPreferences.getInt("insert_kit_cnt", 0);
    Log.d("ehhehe", "rightReviewTiming: " + insertKitCnt);
    if((insertKitCnt % 11 == 0) && (insertKitCnt % 2 == 0)) {
        return true;
    } else {
        return false;
    }
}

I want to update insert_kit_cnt key. But, its log always shows '0'. If you know where is the issue, please let me know about that. I think maybe insert_kit_cnt commit is not working. Is commit timing wrong?


Solution

  • Do the commit or apply on the editor that you're making change on.

    sharedPreferences.edit().putInt("insert_kit_cnt", insertKitCnt).apply();