user2809321
user2809321

Reputation: 204

onSaveInstanceState and onRestoreInstanceState seems not to work

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

Upvotes: 0

Views: 2023

Answers (2)

Rick Falck
Rick Falck

Reputation: 1778

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);
}

Upvotes: 2

Szymon
Szymon

Reputation: 43023

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.

Upvotes: 2

Related Questions