0

我有以下非常简单的片段。

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>

我意识到如果我通过 Java 代码将它附加到 FragmentActivity。

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);        
    }   
}

每次我执行旋转时,我Fragment都能savedInstanceStateonCreate

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

但是,如果我要替换MainActivity's中的代码onCreate

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

通过 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>

当我执行轮换时,我会得到以下日志。

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

为什么会有这样的Fragment行为差异?如果它是通过 XML 创建的,为什么Fragment在轮换期间无法接收非空的 savedInstanceState 包?

4

1 回答 1

0

被实例化的片段必须具有某种唯一标识符,以便在需要销毁和重新创建父活动时可以将其与先前的实例重新关联。这可以通过以下方式提供:

为片段 XML 标记提供 id 可以解决该问题。

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

于 2013-02-26T14:55:15.840 回答