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);
}