【问题标题】:How To store a stack in onSaveInstanceState()?如何在 onSaveInstanceState() 中存储堆栈?
【发布时间】:2015-03-15 23:50:59
【问题描述】:

我的Activity 中有一堆ints,但是当屏幕旋转时它会变空。 我搜索了一下,发现onSaveInstanceState 但似乎我不能这样存储堆栈?它仅适用于主要变量类型,对吗? 我该怎么办 ?有什么想法吗?

【问题讨论】:

    标签: android android-activity stack lifecycle


    【解决方案1】:

    Stack 是一个实现 Serializable 接口的类。

    所以你可以使用

    outState.putSerializable("stack", stack);
    

    将堆栈存储在 onSaveInstanceState(Bundle outState) 中并通过

    Stack<Integer> stack=(Stack<Integer>) savedInstanceState.getSerializable("stack");
    

    【讨论】:

      猜你喜欢
      • 2016-03-05
      • 2013-12-06
      • 2013-12-04
      • 2020-11-17
      • 2015-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      相关资源
      最近更新 更多