捆绑失去的价值观

时间:2016-07-26 20:04:44

标签: java android

我是java的新手。 我正在尝试在屏幕旋转时将变量从OnSaveInstanceState保存到OnCreate。 我已将保存的值记录到savedInstanceState Bundle中。 当屏幕旋转时,OnCreate(Bundle savedInstanceState)中的Bundle值在日志上显示为0,但在OnSaveInstanceState的日志中显示为正确的值。

我的活动Java是

package com.hfad.stopwatch;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class StopwatchActivity extends Activity {

    private static final String TAG = StopwatchActivity.class.getSimpleName();
    private int seconds;
    private boolean running;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stopwatch);
        if(savedInstanceState != null){
            Log.d(TAG, "onCreate() Restoring previous state");
            /* restore state */
            seconds = savedInstanceState.getInt("seconds");
            running = savedInstanceState.getBoolean("running");
            String tmpStr = String.valueOf(seconds);
            Log.d(TAG,tmpStr);
            Log.d(TAG, "onCreate() ending");
        } else {
            Log.d(TAG, "onCreate() No saved state available");
            /* initialize app */
        }
        runTimer();
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        super.onSaveInstanceState(savedInstanceState);
        Log.d(TAG,"onSaveInstanceState() saving state");
        savedInstanceState.putInt("Seconds", seconds);
        savedInstanceState.putBoolean("running", running);
        String tmpStr = String.valueOf(savedInstanceState.getInt("Seconds"));
        Log.d(TAG,tmpStr);
        Log.d(TAG,"onSaveInstanceState() ending");
    }

    //Start the stopwatch running when the start button is clicked
    public void onClickStart(View view){
        running = true;
    }

    //Stop the stopwatch running when the stop button is clicked
    public void onClickStop(View view){
        running = false;
    }

    //Start the stopwatch running when the start button is clicked
    public void onClickReset(View view){
        running = false;
        seconds = 0;
    }

    private void runTimer(){
        final TextView timeView = (TextView)findViewById(R.id.time_view);
        final Handler handler = new Handler();
        handler.post(new Runnable(){
            @Override
            public void run(){
                int hours = seconds/3600;
                int minutes = (seconds%3600)/60;
                int secs = seconds%60;
                String time = String.format("%d:%02d:%02d",hours,minutes,secs);
                timeView.setText(time);
                if(running){
                    seconds++;
                }
                handler.postDelayed(this, 1000);
            }
        });
    }

}

这是日志。

07-26 21:02:19.880 6124-6124/com.hfad.stopwatch D/StopwatchActivity: onSaveInstanceState() saving state

07-26 21:02:19.880 6124-6124/com.hfad.stopwatch D/StopwatchActivity: 11

07-26 21:02:19.880 6124-6124/com.hfad.stopwatch D/StopwatchActivity: onSaveInstanceState() ending

07-26 21:02:19.940 6124-6124/com.hfad.stopwatch D/StopwatchActivity: onCreate() Restoring previous state

07-26 21:02:19.940 6124-6124/com.hfad.stopwatch D/StopwatchActivity: 0

07-26 21:02:19.940 6124-6124/com.hfad.stopwatch D/StopwatchActivity: onCreate() ending

我正在读一本书中的课程,但这不能解释,我无法在线找到解决方案。

3 个答案:

答案 0 :(得分:3)

您使用了错误的密钥来查找该值。密钥中有一个大写SBundle中的密钥区分大小写。

这里的一个好习惯是将密钥字符串声明为最终静态并使用它们来存储和检索。 E.g。

public static final String SECONDS_KEY = "seconds";
public static final String RUNNING_KEY = "running"

答案 1 :(得分:1)

设置要保存的值后,应调用super.onSaveInstanceState(savedInstanceState)

您可以看到更多in the documentations

答案 2 :(得分:1)

Bundle区分大小写。你正在输入“秒”并检索“秒”。好的做法是将键定义为常量,这样就不会遇到这样的错误。

Bundle - is key case sensitive?