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