java - Android 通过 onSavedInstance 方法中的包传递列表

标签 java android

当方向改变时我需要保存一个列表。为此,我想通过 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>)

如何将此数据传递到 bundle 中?我使用 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接口(interface)。在您的情况下,这意味着 EarSrt 必须是 Parceable 因为您正在使用它的 List 。请参阅herehere如何实现Parceable

要恢复数据,使用 onRestoreInstanceStateonCreate 都没有关系。引用自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).

关于java - Android 通过 onSavedInstance 方法中的包传递列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23630336/

相关文章:

android - 当您拥有 AppCompatActivity 和标准库时,为什么要包含最低 SDK 版本?

java - 使用 Google Sheets/Google Drive API 请求特定文件权限

java - Springs Integration的回复关联过程详解

android - 尝试为项目配置 IIS Express 时发生错误。由于权限不足,redirection.config 无法读取文件

android - 应用程序关闭后 NativeScript 后台服务停止工作

android - 为什么我不能在 Retrofit 中向拦截器请求 OkHttp 添加 header ?

java - Spring : how to apply role to existing JWT token

Java IdentityHashMap 的 C# 等价物

java - 如何创建 Maven 可分发

android - 获取 UI 元素值到另一个类