0

I'm trying to make a counter and trying to save the current value when you leave the application, so I tried to use onSaveInstanceState and onRestoreInstanceState but it seems to not to work

code is below

package com.example.taekwondobuddy.util;

import android.app.Activity;

import android.os.Bundle;
import android.view.View;
 import android.widget.Button;
import android.widget.TextView;

public class Counter extends Activity {

int counter;
Button add;
Button sub;
TextView display;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.counter);

    counter = 0;
    add = (Button) findViewById(R.id.button1);
    sub = (Button) findViewById(R.id.button2);
    display = (TextView) findViewById(R.id.tvDisplay);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter++;
            display.setText("Counter: " + counter);
        }
    });
    sub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter--;
            display.setText("Counter: " + counter);
        }
    });
}

public void onSaveInstanceState(Bundle savedInstanceState) {
      super.onSaveInstanceState(savedInstanceState);


      savedInstanceState.putInt("counter", counter);


    }

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);


  counter = savedInstanceState.getInt("counter");

   }



  }

this is my first time i'm overring savedInstanceState so I was wondering if the syntax was right and am I'm using it the right way? If so what's wrong with my code? helps and tips are appreciated

user2809321
  • 196
  • 1
  • 3
  • 17
  • http://stackoverflow.com/questions/6525698/how-to-use-onsavedinstancestate-example-please already this things are example here.. – vinay Maneti Nov 10 '13 at 07:45

2 Answers2

2

You need to swap the order in the methods as the parents' implementation methods will return from the methods and your code won't run. Also, you need to check if the parameter is not null in onRestoreInstanceState.

public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putInt("counter", counter);
    super.onSaveInstanceState(savedInstanceState);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        counter = savedInstanceState.getInt("counter");
    }
    super.onRestoreInstanceState(savedInstanceState);
}

You also said

I'm trying to make a counter and trying to save the current value when you leave the application

Saving instance state only works when the application is in memory (though leaving the application doesn't remove it). If it's killed, the state will be lost.

Szymon
  • 42,577
  • 16
  • 96
  • 114
  • i worked when I went away with home but when I push back (this was part of a menu forgot to say) it didn't saved the instance – user2809321 Nov 10 '13 at 07:51
2

You don't need onRestoreInstanceState(). This is called long after onCreate(), and is usually worthless to apps that need the data in onCreate(). You want to retrieve saved state in onCreate(), which is passed the Bundle too.

In onCreate():

counter = 0;
if (savedInstanceState != null) {
    counter = savedInstanceState.getInt("counter", 0);
}
Rick Falck
  • 1,778
  • 3
  • 15
  • 19