Reputation: 890
I want to save some values with SharedPreferences in my app. These values constantly change when it is active (for example every game brings some coins, and I want to save these coins). However I don't know when the user will quit the app in order to save the coins then for next time. So in every activity where the coins change I have:
@Override
protected void onStop() {
super.onStop();
SharedPreferences sp = getSharedPreferences("my_pref", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("coins", MainActivity.COINS);
editor.commit();
}
Is there a way to do this better.
Upvotes: 1
Views: 39
Reputation: 7104
use isFinishing() in onPause() method that means that the application paused and if isFinishing() is true then your app will end
@Override
protected void onPause() {
super.onPause();
if(isFinishing())
{
//finishing logic here
}
}
Upvotes: 1