Android 视图不考虑 Bundle 状态,正在重新创建时忽略 Bundle 信息
Android View is not respecting Bundle state, being recreated disregarding Bundle information
View 没有使用自动生成的 Bundle 信息。
搜索 Stack Overflow 未找到相关结果。视图不是自定义的,具有唯一的名称 ID。不是重复的:
findViewById not working for an include?
FindViewById() not finding View
Android: findviewbyid: finding view by id when view is not on the same layout invoked by setContentView
其他视图按预期工作。唯一没有添加为包含,这是我能找到的唯一区别。
问题描述:视图被隐藏,Activity被重新创建,视图被显示。
详细信息: 我有一个按钮,它使用默认的 android:onClick
属性,调用一个方法。此方法按预期工作。然后,我 "flip" Android 设备,方向改变,导致它调用 onSaveInstanceState(Bundle b)
。有关 Activity 的信息被捆绑,Activity 被销毁并重新创建,然后保存的 Bundle 按预期发送到 onCreate(Bundle b)
。
然后,根据 Android Activity,Bundle 恢复视图状态。
单个视图除外
class ExampleActivity extends AppCompatActivity {
public void hide(View view){
view.setVisibility(View.GONE);
}
}
Activity XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/linear_layout_style"
tools:context=".ExampleActivity">
<View style="@style/another_style" />
<include layout="@layout/included_xml" />
<View style="@style/another_style" />
</LinearLayout>
Included XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/linear_layout_style">
<!-- This was supposed to stay hidden after pressed, but it restores in a VISIBLE state -->
<Button
style="@style/the_style"
android:id="@+id/an_unique_id"
android:onClick="hide"
android:text="EXAMPLE: Hide" />
<!-- This is supposed to always be visibile, and it does -->
<Button
style="@style/the_style"
android:onClick="doOtherStuff"
android:text="Example: Always Available">
<requestFocus />
</Button>
</LinearLayout>
已保存的实例不会保存所有内容。我查看了 TextView
的来源(Button
extends and is not much more more than a TextView
)并且它没有保存可见性。事实上,它唯一保存的是文本和文本选择,但前提是你调用 setFreezesText(true)
就可以了。出于同样的原因,View.onSaveInstanceState()
不做任何事情(只是 returns 一个空的保存状态)。
因此,您需要自己手动保存和恢复它们。我认为 Android 在这方面做得很差。文档说:
The default implementation takes care of most of the UI per-instance
state for you by calling View.onSaveInstanceState() on each view in
the hierarchy that has an id, and by saving the id of the currently
focused view (all of which is restored by the default implementation
of onRestoreInstanceState(Bundle)).
在查看平台源代码后,这肯定看起来具有误导性。我的意思是,这是 Nougat 中 View.onSaveInstanceState()
的实现:
protected Parcelable onSaveInstanceState() {
mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
if (mStartActivityRequestWho != null) {
BaseSavedState state = new BaseSavedState(AbsSavedState.EMPTY_STATE);
state.mStartActivityRequestWhoSaved = mStartActivityRequestWho;
return state;
}
return BaseSavedState.EMPTY_STATE;
}
我不同意这是 "takes care of most" 的作品。
View 没有使用自动生成的 Bundle 信息。
搜索 Stack Overflow 未找到相关结果。视图不是自定义的,具有唯一的名称 ID。不是重复的:
findViewById not working for an include?
FindViewById() not finding View
Android: findviewbyid: finding view by id when view is not on the same layout invoked by setContentView
其他视图按预期工作。唯一没有添加为包含,这是我能找到的唯一区别。
问题描述:视图被隐藏,Activity被重新创建,视图被显示。
详细信息: 我有一个按钮,它使用默认的 android:onClick
属性,调用一个方法。此方法按预期工作。然后,我 "flip" Android 设备,方向改变,导致它调用 onSaveInstanceState(Bundle b)
。有关 Activity 的信息被捆绑,Activity 被销毁并重新创建,然后保存的 Bundle 按预期发送到 onCreate(Bundle b)
。
然后,根据 Android Activity,Bundle 恢复视图状态。
单个视图除外
class ExampleActivity extends AppCompatActivity {
public void hide(View view){
view.setVisibility(View.GONE);
}
}
Activity XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/linear_layout_style"
tools:context=".ExampleActivity">
<View style="@style/another_style" />
<include layout="@layout/included_xml" />
<View style="@style/another_style" />
</LinearLayout>
Included XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/linear_layout_style">
<!-- This was supposed to stay hidden after pressed, but it restores in a VISIBLE state -->
<Button
style="@style/the_style"
android:id="@+id/an_unique_id"
android:onClick="hide"
android:text="EXAMPLE: Hide" />
<!-- This is supposed to always be visibile, and it does -->
<Button
style="@style/the_style"
android:onClick="doOtherStuff"
android:text="Example: Always Available">
<requestFocus />
</Button>
</LinearLayout>
已保存的实例不会保存所有内容。我查看了 TextView
的来源(Button
extends and is not much more more than a TextView
)并且它没有保存可见性。事实上,它唯一保存的是文本和文本选择,但前提是你调用 setFreezesText(true)
就可以了。出于同样的原因,View.onSaveInstanceState()
不做任何事情(只是 returns 一个空的保存状态)。
因此,您需要自己手动保存和恢复它们。我认为 Android 在这方面做得很差。文档说:
The default implementation takes care of most of the UI per-instance state for you by calling View.onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState(Bundle)).
在查看平台源代码后,这肯定看起来具有误导性。我的意思是,这是 Nougat 中 View.onSaveInstanceState()
的实现:
protected Parcelable onSaveInstanceState() {
mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
if (mStartActivityRequestWho != null) {
BaseSavedState state = new BaseSavedState(AbsSavedState.EMPTY_STATE);
state.mStartActivityRequestWhoSaved = mStartActivityRequestWho;
return state;
}
return BaseSavedState.EMPTY_STATE;
}
我不同意这是 "takes care of most" 的作品。