I need to save a list when the orientation changes. To do this I want to pass the data in through the onSavedInstanceState
method but, I don't no how to go about doing this, please see example code below:
private List<EarSrt> leftAnswerList;
private List<EarSrt> rightAnswerList;
@Override
protected void onSaveInstanceState (Bundle outState){
super.onSaveInstanceState(outState);
outState.putCharSequenceArrayList("left_ear", leftAnswerList);
outState.putCharSequenceArrayList("right_ear", rightAnswerList);
}
This code doesn't work as I'm passing in a custom list.
error: The method putCharSequenceArrayList(String, ArrayList<CharSequence>) in the type Bundle is not applicable for the arguments (String, List<EarSrt>)
How can I pass this data into the bundle? Do I use a Parcelable
? If so, how would I go about using a Parcelable
in the onSavedStateChanged
method?
Also How to I retrieve the data do I use the onRestoreInstanceState
method? If so, How would I go about doing this?
Thanks for any help.
Edit:
So I've just realised that I can pass it in through the below code:
@Override
protected void onSaveInstanceState (Bundle outState){
super.onSaveInstanceState(outState);
outState.putParcelable("left_ear", (Parcelable) leftAnswerList);
outState.putParcelable("right_ear", (Parcelable) rightAnswerList);
}
Do I need to use the onRestoreInstanceState
method or can I just say in the onCreate
method if(savedInstanceState != null)
then do something?
Edit:
onCreate Method:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hearing_test);
ActivityHelper activityHelper = new ActivityHelper(this);
activityHelper.setTitleTextSize(R.string.Hearing_Test, true);
isPhone = activityHelper.isPhone();
if(isPhone){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
View infoView = (View)findViewById(R.id.info_button);
infoView.setVisibility(View.INVISIBLE);
notUnderstoodButton = (Button) findViewById(R.id.not_understood_button);
repeatButton = (Button) findViewById(R.id.repeat_button);
progressTextView = (TextView) findViewById(R.id.progressTextView);
progressTextView.setText("Left ear: Step 1 of 9");
notUnderstoodButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
soundButtonClicked(v);
}
});
repeatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
noisePlayer.seekTo(0);
testPlayer.seekTo(0);
noisePlayer.start();
testPlayer.start();
disableButtons();
}
});
panLeft = false;
initialiseArrays();
getNextTest(-1);
}
To pass your data via a Bundle
, it need to implement the Parceable
interface. In your case it means that EarSrt
has to be a Parceable
because you are using a List
of it. See here or here how to implement a Parceable
.
To restore your data, it doesn't matter, if onRestoreInstanceState
or onCreate
is used. Quote from the doc :
This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState(Bundle).
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.