繁体   English   中英

通过onSavedInstance方法中的捆绑包获取Android通行证列表

[英]Android pass list via bundle in onSavedInstance method

方向更改时,我需要保存一个列表。 为此,我想通过onSavedInstanceState方法传递数据,但是我不知道如何执行此操作,请参见下面的示例代码:

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

当我传递自定义列表时,此代码不起作用。

错误: The method putCharSequenceArrayList(String, ArrayList<CharSequence>) in the type Bundle is not applicable for the arguments (String, List<EarSrt>)

如何将这些数据传递到捆绑包中? 我是否使用Parcelable 如果是这样,我怎么会去使用ParcelableonSavedStateChanged方法?

另外,如何使用onRestoreInstanceState方法检索数据? 如果是这样,我将如何去做?

谢谢你的帮助。

编辑:

因此,我刚刚意识到可以通过以下代码传递它:

@Override
protected void onSaveInstanceState (Bundle outState){
    super.onSaveInstanceState(outState);
    outState.putParcelable("left_ear", (Parcelable) leftAnswerList);
    outState.putParcelable("right_ear", (Parcelable) rightAnswerList);

}

我需要使用onRestoreInstanceState方法还是可以在onCreate方法中说if(savedInstanceState != null)然后执行某些操作?

编辑:

onCreate方法:

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

要通过Bundle传递数据,它需要实现Parceable接口。 在您的情况下,这意味着EarSrt必须是Parceable因为您正在使用它的List 在此处此处查看如何实现Parceable

如果使用onRestoreInstanceStateonCreate ,则恢复数据无关紧要。 文档引用:

当活动从先前保存的状态重新初始化时,在onStart()之后调用此方法(在此处已保存状态)。 大多数实现将仅使用onCreate(Bundle)来恢复其状态,但是在完成所有初始化或允许子类决定是否使用默认实现之后,有时在此处执行此操作很方便。 此方法的默认实现将还原以前由onSaveInstanceState(Bundle)冻结的任何视图状态。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM