Android - how to differentiate activity restart from a "normal" start
I'm trying to distinguish if an activity is destroyed then restarted vs. when it's started via standard startActivity call. What's the best way to distinguish this? I need to track a counter, and the counter should not be incremented when the app has been destr开发者_如何学Gooyed and restarted. I tried using putExtra(String, String), but the value returned is the same regardless.
You could try using a flag isSavedInstanceState. Set this flag to false in onResume. Set this flag to true in onSaveInstanceState. Check this flag in onStop.
@Override
protected void onStop(){
super.onStop();
if (!isSavedInstanceState){ // this is a HARD KILL, write to prefs
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putYourCounterIncrementHere
editor.commit();
Log.d(TAG,"savedPrefs");
}
else {
Log.d(TAG,"DidNotSavePrefs");
}
Log.d(TAG,"onStop");
}
This will increment your counter on a hard kill. You could check the bundle in onCreate for a null bundle if you want, but I have not fully tested that logic.
精彩评论