我刚刚开始开发安康平台。 就我正在处理的申请而言,我正在使用一个包含以下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>
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();
活动成绩良好,问题来自援助。 在扩大优惠贷款的类别中,我实施了Save InstanceState(Save Instance State)()。
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.