English 中文(简体)
Fra Fra(null Bundle)
原标题:Android no persistence (null Bundle) in Fragment

我刚刚开始开发安康平台。 就我正在处理的申请而言,我正在使用一个包含以下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.

问题回答

我认为<代码>onSaveInstanceState。 如果碎片/活动后来被销毁(例如,本组织恢复记忆),则呼吁更是先发制人,需要由系统恢复。 由于你们的碎块被销毁并重整,没有必要从被挽救的国家恢复。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签