简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM