Android clears SharedPreferences generated data of instance code

  • 2020-05-19 05:47:42
  • OfStack



  Definition: 
        SharedPreferences preferences = null;
 SharedPreferences.Editor editor = null;
  preferences = getSharedPreferences(TAG, Activity.MODE_PRIVATE);
  editor = preferences.edit();
 in onstop Inside save play position 
 @Override
 protected void onStop() {
  editor.putInt(filePath, currentposition);
  //  Submit the saved results 
  Log.e(TAG, "onStop");
  editor.commit();
  super.onStop();
 }
 in onPrepared In the seekto To the original position 
public void onPrepared(MediaPlayer mp) {

 currentposition = preferences.getInt(filePath, -1);
  if (currentposition != -1) {
   mUvv.seekTo(currentposition);
   }
 Later, what I need is to clear these records ondestroy In the clear.   It should be noted that the data should also be submitted when it is cleaned, otherwise, the data will remain the same until it is refreshed, even if it was not noticed at the beginning commit . It's getting me down... 
 protected void onDestroy() {
  // TODO Auto-generated method stub
  if (clearshared) {
   editor.clear();
   editor.commit();
  }
  super.onDestroy();
 }


Related articles: