【问题标题】:Android Screen goes blank on orientation change; onConfigurationChanged is calledAndroid 屏幕在方向更改时变为空白; onConfigurationChanged 被调用
【发布时间】:2013-04-23 06:52:25
【问题描述】:

当我的活动(带有两个选项卡的选项卡活动)方向发生更改时,选项卡的内容,这是另一个具有项目列表视图的活动(带复选框),变为空白。我在清单文件中使用了android:configChanges="screenSize|orientation",因此我不希望在方向更改时清除(或重新创建)复选框。

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: android android-activity


    【解决方案1】:

    为了在 Android 中保存 View 的状态,您需要实现 onSaveInstanceState(Bundle outState)onRestoreInstanceState(Bundle savedInstanceState) 方法。有关这些方法的更多信息,请参阅Android Developer Guide

    onSaveInstanceState(Bundle outState) 在配置更改(方向更改)时销毁活动时调用。您可以在此方法中将数据添加到包中,然后在使用onRestoreInstanceState(Bundle savedInstanceState) 重新启动活动时检索它。

    例如:

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        // Read values from the "savedInstanceState" bundle and put them back into the corresponding textviews
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Save the values you need from your textviews into the outState object
    }
    

    有关如何将数据保存到包中的更多详细信息,请参阅Android Developer Guide。这也解释了使用onCreate(Bundle savedInstanceState)onRestoreInstanceState(Bundle savedInstanceState) 恢复状态之间的区别。

    【讨论】:

    • 如果这解决了您的问题,请投票并接受作为答案。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多