简体   繁体   中英

Fragment savedInstanceState is ALWAYS null

I have statically created a fragment (via XML). I'm trying to store the last displayed value in a bundle and display it whenever the app is started next. However I am not able to get it to work. For some reason savedInstanceState is always null.

public class DistanceSetterFragment extends Fragment implements SharedPreferences.OnSharedPreferenceChangeListener { 
    Distance distance = new Distance();

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        if (savedInstanceState!=null )
        {
            Log.d(this.getClass().getName(),"onCreate savedInstanceState is NOT null");

        }
        else
        {
            Log.d(this.getClass().getName(),"onCreate savedInstanceState is null");
        }


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        Log.d(this.getClass().getName(),"Distance "+distance);


        if (savedInstanceState!=null )
        {
            Log.d(this.getClass().getName(),"onCreateView savedInstanceState is NOT null");

        }
        else
        {
            Log.d(this.getClass().getName(),"onCreateView savedInstanceState is null");
        }


        return inflater.inflate(R.layout.fragment_distancesetter, container, false);
    }



    @Override
    public void onSaveInstanceState(Bundle outState) 
    {
        super.onSaveInstanceState(outState);

        if (distance!=null) {
            Log.d(this.getClass().getName(),"Saving DISTANCE_BEAN "+distance);
            outState.putSerializable(Constants.DISTANCE_BEAN, distance);
        }
        else
        {
            Log.d(this.getClass().getName(),"Distance BEAN IS NULL");

        }

        outState.putString("", "");

    }
}

Below is the fragment XML declared in my main activity XML

<fragment
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/fragment_distancesetter"
    android:layout_below="@id/img_logo_main"
    android:name="com.webconfs.xyz.fragments.DistanceSetterFragment"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    />

As you can see - I have NOT set setRetainInstance(true) in my Fragment class and - My fragment XML has an ID associated with it android:id="@+id/fragment_distancesetter

I had the same problem just now and it was driving me crazy, but as it turns out the fragment was simply being re-added in the activity with every rotation. You didn't add your Activity's code, but you might want to check this is not the case as it's easy to overlook and would explain your problem.

When you put a fragment into an activity statically, the fragment manager will always create a new fragment and attach it to activity. The restoreInstanceState() method will never be called. If you want to do it, you can save state in your activity's restore method, and put save state of your activity to your fragment. Or create fragment dynamically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM