2016-07-26 113 views
0

我是新来的java。 我试图在屏幕旋转时将变量从OnSaveInstanceState保存到OnCreate。 我已将登录的值保存到savedInstanceState包中。 当屏幕旋转时,在日志中显示的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 

我在一本书中学习下面的内容,但是这不起作用,并且我无法在线找到解决方案。

+0

你应该标记为接受,如果它帮助你的答案之一。请遵循SO的规则。 – Intern

回答

1

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

你可以看到更多的in the documentations

3

您使用了错误的键查找值。钥匙中有一个首都SBundle中的密钥区分大小写。

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

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

谢谢,我会牢记这一点。 – fatmanming

1

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

Bundle - is key case sensitive?

+0

谢谢,下面的例子让我对打字错误一无所知。 – fatmanming