简体   繁体   中英

Why Fragment unable receive a non-null savedInstanceState bundle during rotation, if it is created through XML?

I have the following very simple Fragment.

UiFragment.java

package com.example.test;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockFragment;

/**
 * This is a fragment showing UI that will be updated from work done
 * in the retained fragment.
 */
public class UiFragment extends SherlockFragment {


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Log.i("CHEOK", "UiFragment onActivityCreated");

        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Log.i("CHEOK", "UiFragment onCreateView");

        View v = inflater.inflate(R.layout.uifragment, container, false);

        return v;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i("CHEOK", "UiFragment onCreate");

        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            Log.i("CHEOK", "--> UiFragment able get savedInstanceState's data : " + savedInstanceState.getInt("value"));
        }       
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("value", 111222);        
    }    
}

uifragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="fragment_retain_instance_msg" />

</LinearLayout>

I realize if I were attach it to FragmentActivity through Java code.

MainActivity.java

package com.example.test;

import android.os.Bundle;
import android.util.Log;

import com.actionbarsherlock.app.SherlockFragmentActivity;

public class MainActivity extends SherlockFragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i("CHEOK", "MainActivity onCreate");

        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            Log.i("CHEOK", "--> MainActivity able get savedInstanceState's data : " + savedInstanceState.getInt("value"));
        }

        // ???
        //setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().add(android.R.id.content,
                new UiFragment()).commit();
        }       
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("value", 888999);        
    }   
}

Everytime when I perform rotation, my Fragment able to get non-null savedInstanceState during onCreate

02-26 00:36:48.777: I/CHEOK(17740): MainActivity onCreate
02-26 00:36:48.812: I/CHEOK(17740): UiFragment onCreate
02-26 00:36:49.031: I/CHEOK(17740): UiFragment onCreateView
02-26 00:36:49.035: I/CHEOK(17740): UiFragment onActivityCreated
[Rotation happens here]
02-26 00:37:37.472: I/CHEOK(17740): MainActivity onCreate
02-26 00:37:37.472: I/CHEOK(17740): UiFragment onCreate
02-26 00:37:37.472: I/CHEOK(17740): --> UiFragment able get savedInstanceState's data : 111222
02-26 00:37:37.472: I/CHEOK(17740): --> MainActivity able get savedInstanceState's data : 888999
02-26 00:37:37.511: I/CHEOK(17740): UiFragment onCreateView
02-26 00:37:37.511: I/CHEOK(17740): UiFragment onActivityCreated

However, if I were replace the code in MainActivity 's onCreate

if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction().add(android.R.id.content,
        new UiFragment()).commit();
}

with fragment creation through XML

setContentView(R.layout.activity_main);

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <fragment class="com.example.test.UiFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

I will get the following log when I perform rotation.

02-26 00:39:23.437: I/CHEOK(18055): MainActivity onCreate
02-26 00:39:23.734: I/CHEOK(18055): UiFragment onCreate
02-26 00:39:23.734: I/CHEOK(18055): UiFragment onCreateView
02-26 00:39:23.754: I/CHEOK(18055): UiFragment onActivityCreated
[Rotation happens here]
02-26 00:39:27.703: I/CHEOK(18055): MainActivity onCreate
02-26 00:39:27.703: I/CHEOK(18055): --> MainActivity able get savedInstanceState's data : 888999
02-26 00:39:27.746: I/CHEOK(18055): UiFragment onCreate
02-26 00:39:27.746: I/CHEOK(18055): UiFragment onCreateView
02-26 00:39:27.750: I/CHEOK(18055): UiFragment onActivityCreated

Why there is such difference in Fragment behavior? Why Fragment unable receive a non-null savedInstanceState bundle during rotation , if it is created through XML?

The fragment being instantiated must have some kind of unique identifier so that it can be re-associated with a previous instance if the parent activity needs to be destroyed and recreated. This can be provided these ways:

Supplying id to fragment XML tag solve the problem.

<fragment class="com.example.test.UiFragment"
    android:id="@+id/myFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

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