title: How to save int in SharedPreferences c# when app is going to be closed?
body: <p>I want to save number of clicks in SharedPreferences in moment when app going closed. Now I have function for that connected with one button</p>
  
  <pre><code>   private void SaveClicks (){
      var prefs = Application.Context.GetSharedPreferences("Name",FileCreationMode.Private);
                   var prefEditor = prefs.Edit();
                   prefEditor.PutInt("Key", nametest);
                   prefEditor.Apply();
  
      }
  </code></pre>
  
  <p>"clicks" is name of int where I storage numbers of clicks in one of buttons</p>
  
  <p>In what way I can do it automatically when app is going closed? Using onDestroy will be good solution?</p>
  
  <p>//update
  So I wrote that code:</p>
  
  <pre><code>protected override void OnDestroy()
          {
              var prefs = Application.Context.GetSharedPreferences("Name", FileCreationMode.Private); // 1
              var prefEditor = prefs.Edit(); // 2
              prefEditor.PutInt("Key", nametest); // 3
              prefEditor.Apply(); // 4
  
  
          }
  </code></pre>
  
  <p>And for counting clicks I have something like that </p>
  
  <pre><code>var prefs = Application.Context.GetSharedPreferences("Name", FileCreationMode.Private); // 1
              var value1 = prefs.GetInt("key", 0);
              if (clicks + value1 &lt;= 499)
              {
                  clicks++;
                  textViewBattlepackCount.Text = (clicks + value1).ToString() + " clicks!";
                  progressBarName1.Progress = progressBarName1.Progress + 1;
                  nametest= clicks + value1;
  
  
                  if (clicks + value1 == 500)
                  {
                      AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
                      alertDialog.SetTitle("You won!");
                      alertDialog.SetMessage("message");
                      alertDialog.SetNeutralButton("Ok", delegate
                      {
  
                          alertDialog.Dispose();
  
                      });
                      alertDialog.Show();
  
                      clicks = 0;
                      nametest= 0;
  
                      textViewTXTCount.Text = "0";
                      progressBarName1.Progress = progressBarName1.Progress = 0;
  
                  }
  </code></pre>
  
  <p>But the clicks in onDestroy() are storage only sometimes, one time there is correct number but another time after kill activity and restart app there is old number of clicks. I dont know why. 
  Sorry for chaotic description</p>