ran*_*ity 11 android android-fragments
我有一个带有操作栏标签的活动.每个选项卡包含一个片段.现在当我旋转我的设备时,我相应片段中的bundle将变为null.当我使用设备发布android 3.2时,这很小心,但它是在设备是Andoird3.0时发生的.在解决这个问题之后我很头疼.我在SO上检查了各种链接,但没有帮助.虽然我已经提供了足够的细节,但仍会提供一些代码片段,因为在用户要求代码片段的各种情况下.
在我的片段类中,我存储了这个值
@Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putBoolean("textboxVisible", true);
}
Run Code Online (Sandbox Code Playgroud)
这是存储一个布尔变量,它按如下方式重新检索.
/**
* Function called after activity is created. Use this
* method to restore the previous state of the fragment
*/
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null)
{
//restore the state of the text box
boolean textboxVisible = savedInstanceState.getBoolean("textboxVisible");
if (textboxVisible)
{
//do some stuff
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是在旋转后,savedInstanceState将变为 null.我不是出了什么问题.我在一些文档中读到,在3.2以下,片段的onCreateView()没有使用bundle值调用.但要解决这个问题.任何帮助将不胜感激.
尝试savedInstanceState进入.onCreate Fragment喜欢
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
if (savedInstanceState != null) {
// IT MUST NOT BE NULL HERE
}
}
Run Code Online (Sandbox Code Playgroud)
请尝试...我希望它会起作用