Android : Why wont integer variable increment by 1 everytime activity is opened?

on Sunday, October 12, 2014


In my activity below I have an integer called test. I want this integer to add 1 to itself every time the activity is opened and then get a toast to display its value. I have done this but every time I open the activity the value is always printed in the toast as 1. Why is this?



int test = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hangar);

final SharedPreferences saving = getSharedPreferences(FILE_NAME, Activity.MODE_PRIVATE);
final SharedPreferences.Editor editor = saving.edit();

test = saving.getInt("testing", 0);

test++;
Toast.makeText(this, "Hello There " + test, Toast.LENGTH_SHORT).show();
editor.putInt("testing", test);

0 comments:

Post a Comment