Android 在 Fragment 中没有持久化(null Bundle)
我刚刚开始开发Android平台。对于我正在开发的应用程序,我使用具有以下 .xml 文件的活动来管理应用程序属性。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment class="fr.berard.preference.ManagePreferenceActivity$PreferenceMenuFragment"
android:id="@+id/titles" android:layout_weight="20"
android:layout_width="0px" android:layout_height="match_parent" />
<FrameLayout android:id="@+id/details" android:layout_weight="80"
android:layout_width="0px" android:layout_height="match_parent"
android:background="?android:attr/detailsElementBackground" />
</LinearLayout>
当用户从 PreferenceMenuFragment 中选择一个元素时,详细信息片段将通过以下几行加载:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
PreferenceFragment pdf = new CustomPreferenceFragment();
fragmentTransaction.replace(R.id.details, pdf);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
活动工作正常,问题来自于持久性。我在扩展 PreferenceFragment 的类中实现了 onSaveInstanceState() 。
public final String WRONG_CARD_KEY = "wrong_card_value";
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
if (savedInstanceState.containsKey(WRONG_CARD_KEY)) {
Toast.makeText(this.getActivity(), " loaded", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString(WRONG_CARD_KEY, "test");
Toast.makeText(this.getActivity()," saved", Toast.LENGTH_SHORT).show();
}
当我按下主页按钮时,将调用 onSaveInstanceState() 方法。但是当我返回 PreferenceFragment 时,调用了 onActivityCreated() 但该包仍然为空。 我无法解释为什么我放入 onSaveInstanceState 的信息没有保存。也许我在实现我的片段时犯了一个错误,也许它来自我在主要活动中启动它的方式。 我发现一些帖子与 ListFragment 有同样的问题,但没有答案。
感谢您的帮助。
I just started to develop for Android platform. For the application I am working on, I am using an activity with the following .xml file in order to manage the application properties.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment class="fr.berard.preference.ManagePreferenceActivity$PreferenceMenuFragment"
android:id="@+id/titles" android:layout_weight="20"
android:layout_width="0px" android:layout_height="match_parent" />
<FrameLayout android:id="@+id/details" android:layout_weight="80"
android:layout_width="0px" android:layout_height="match_parent"
android:background="?android:attr/detailsElementBackground" />
</LinearLayout>
When the user select an element from PreferenceMenuFragment, the details fragment is loaded by the following lines:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
PreferenceFragment pdf = new CustomPreferenceFragment();
fragmentTransaction.replace(R.id.details, pdf);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
The activity works fine, the problem is comes from the persistance. I implemented onSaveInstanceState() in the class extanding PreferenceFragment.
public final String WRONG_CARD_KEY = "wrong_card_value";
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
if (savedInstanceState.containsKey(WRONG_CARD_KEY)) {
Toast.makeText(this.getActivity(), " loaded", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString(WRONG_CARD_KEY, "test");
Toast.makeText(this.getActivity()," saved", Toast.LENGTH_SHORT).show();
}
When when I press the home button, the onSaveInstanceState() method is called. But when I go back to the PreferenceFragment, onActivityCreated() is called but the bundle is still null.
I can't explain why the informations i put in onSaveInstanceState are not saved. Maybe it I made a mistake while implementing my Fragment, maybe it is comming from the way I start it in the main activity.
I found some posts with the same problem with ListFragment but without answer.
Thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 onSaveInstanceState 调用更像是一种抢占式调用,以防片段/活动随后被销毁(例如由操作系统回收内存)并且需要由系统恢复。由于您的片段没有被销毁并重新创建,因此它不需要从保存的状态恢复自身。
I think the
onSaveInstanceState
call is more of a pre-emptive call in case the fragment/activity is subsequently destroyed (say by the OS reclaiming memory) and needs to be restored by the system. As your fragment isn't being destroyed and recreated it doesn't need to restore itself from the saved state.