Reputation: 185
I'm new to java.
I'm trying to save variables from OnSaveInstanceState
, to OnCreate
when the screen rotates.
I've logged the values saved into the savedInstanceState
Bundle.
When the screen rotates the Bundle values in OnCreate(Bundle savedInstanceState)
show on the log as 0's, but show as the correct values in the log for OnSaveInstanceState
.
My Activity Java is
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);
}
});
}
}
This is the Log.
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
I'm following lessons in a book, but this does not work as explained, and I can't find a solution online.
Upvotes: 0
Views: 63
Reputation: 2826
Bundle is case-sensitive. You are putting in "Seconds" and retrieving "seconds". Good practice is to define the keys as constants so you don't experience such a mistake.
Bundle - is key case sensitive?
Upvotes: 1
Reputation: 5640
You're using a wrong key to find the value. There is a capital S
in your key. Keys in a Bundle
are case sensitive.
A good practice here would be to declare the key string as a final static and use them to store and retrieve. E.g.
public static final String SECONDS_KEY = "seconds";
public static final String RUNNING_KEY = "running"
Upvotes: 3
Reputation: 1052
You should call super.onSaveInstanceState(savedInstanceState)
after set the values you want to save.
You can see more in the documentations
Upvotes: 1