Mkrasek
Mkrasek

Reputation: 13

How to save int in SharedPreferences c# when app is going to be closed?

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

   private void SaveClicks (){
    var prefs = Application.Context.GetSharedPreferences("Name",FileCreationMode.Private);
                 var prefEditor = prefs.Edit();
                 prefEditor.PutInt("Key", nametest);
                 prefEditor.Apply();

    }

"clicks" is name of int where I storage numbers of clicks in one of buttons

In what way I can do it automatically when app is going closed? Using onDestroy will be good solution?

//update So I wrote that 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


        }

And for counting clicks I have something like that

var prefs = Application.Context.GetSharedPreferences("Name", FileCreationMode.Private); // 1
            var value1 = prefs.GetInt("key", 0);
            if (clicks + value1 <= 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;

                }

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

Upvotes: 1

Views: 351

Answers (2)

Mike Ma
Mike Ma

Reputation: 2027

For your next question.

The SharedPreferences "key" should be covered when you call onDestory(), If not, please track the value of your "nametest" using debug

BTW

  prefEditor.PutInt("Key", nametest);
  var value1 = prefs.GetInt("key", 0);

Please using the same "Key" - -!!

Upvotes: 0

Mike Ma
Mike Ma

Reputation: 2027

It depends your requirement:

  1. If u just want to store the numbers of click when the activity be killed. Using OnDestory()

  2. If u want to store the numbers of click when the activity is in the background(e.g. you press the home button or another activity started), please using onPause();

  3. onSaveInstanceState() can also be invoked before the activity destroyed, it can restore some temporary data (e.g. the text in EditText).

Upvotes: 1

Related Questions