【问题标题】:Android CheckBox remains state after destroyAndroid CheckBox 在销毁后保持状态
【发布时间】:2013-09-04 08:56:25
【问题描述】:

我确实有一个非常简单的应用程序,只有一个 CheckBox。我认为当 android 确实破坏了我的活动时,此复选框的状态将是列表。实际发生的是,复选框保持选中状态。

实际上调用了 onDestroy()(在轮换或切换到另一个应用程序时 -> 我的模拟器具有此开发配置)。

那么为什么不使用 onRestoreInstanceState() 和 onSaveInstanceState() 就可以工作呢?

Android 目标是 4.2 Jelly Bean。

这是我的活动:

public class CoffeeMixerActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_coffee_mixer);
}

protected void onDestroy() {
    super.onDestroy();

    Log.d("MyMessage", "Destroy my app");
}
}

这是我的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".CoffeeMixerActivity" >

<CheckBox
    android:id="@+id/checkBoxSprinkles"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Sprinkles" />

</RelativeLayout>

【问题讨论】:

    标签: android android-activity


    【解决方案1】:

    好的..自己找到了解决方案: Android CheckBox -- Restoring State After Screen Rotation

    说明: 每个 View 都有 android:saveEnabled 标志为 true .. 这意味着当 View 确实有 ID 时,状态会在视图冻结时自动保存...

    这里有属性文档: http://developer.android.com/reference/android/R.attr.html#saveEnabled

    【讨论】:

      猜你喜欢
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      相关资源
      最近更新 更多