Sharepreference to store int value

Viewed 63

I have int value and I want it to increases by 1 when we click positive or negative button of Alert dialogue, and store the int value even when the user closes the app. I've done these but I don't know why is this not working.

int counter;

in the oncreate

initA();
private void initA(){

if(getCounter() < 1)
{makeAlertDialogOther();}
}
private void makeAlertDialogOther() {
        new AlertDialog.Builder(getActivity())
                .setMessage(Constant.SETTINGS.getEntranceMsg())
                .setCancelable(false)
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        counterU();

                    }
                })
                .setPositiveButton("Update", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        counterU();
                    }
                })
                .show();
    }

This is where I made sharepreference:

 private void counterU() {
        sp = getActivity().getSharedPreferences("MyPrfs", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        int oldCounter = sp.getInt("counterValue", 0);
        editor.putInt("counterValue", oldCounter + 1);
        editor.apply();
    }
private int getCounter() {
        sp = getActivity().getSharedPreferences("MyPrfs", Context.MODE_PRIVATE);
        return sp.getInt("counterValue", 0);
    }
1 Answers

The reason your code not working: Whenever you close and open screen again, you start to save a new counter value (start from 0) again to your SharedPreferences.
The solution: Whenever we start to save a counter to SharedPreferences, we get the old value of counter in SharedPreferences first then increase and save back.

private void initA() {
     if(getCounter() < 1) {
        makeAlertDialogOther();
     }
}

private void makeAlertDialogOther() {
    new AlertDialog.Builder(getActivity())
        .setMessage(Constant.SETTINGS.getEntranceMsg())
        .setCancelable(false)
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                counterU();
            }
        })
        .setPositiveButton("Update", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                counterU();
            }
        })
        .show();
}

private void counterU() {
    sp = getActivity().getSharedPreferences("MyPrfs", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    int oldCounter = sp.getInt("counterValue", 0);
    editor.putInt("counterValue", oldCounter + 1);
    editor.apply();
}

private int getCounter() {
    sp = getActivity().getSharedPreferences("MyPrfs", Context.MODE_PRIVATE);
    return sp.getInt("counterValue", 0);
}
Related