片段生命周期:使用后退按钮时未调用onCreateView()

时间:2013-05-23 11:19:54

标签: android android-fragments

编辑:经过一些实验,如果我不在xml布局中添加初始片段,它看起来像预期的那样工作。我现在正在使用我的活动源代码。我想这是我应该怎么做的?

根据http://developer.android.com/guide/components/fragments.html#Creating,如果删除一个片段然后再添加,则应调用onCreateView()。

我也可以看到getView()返回null。 调用onDestroyView()但是按下后退时仍会显示我的第一个片段的界面

这是我的示例代码的结果:

--launch app
I/System.out( 3765): ==== FRAGMENT1.ONCREATE
I/System.out( 3765): ==== FRAGMENT1.ONCREATEVIEW
I/System.out( 3765): ==== FRAGMENT1.ONACTIVITYCREATED (FRAGMENT1.getView = android.support.v4.app.NoSaveStateFrameLayout@41301268

--second fragment
I/System.out( 3765): ==== FRAGMENT2.ONCREATE
I/System.out( 3765): ==== FRAGMENT2.ONCREATEVIEW
I/System.out( 3765): ==== FRAGMENT2.ONACTIVITYCREATED

--back is pressed : getView() == null and onCreateView is not called
I/System.out( 3765): ==== FRAGMENT1.ONACTIVITYCREATED (FRAGMENT1.getView = null

如果我做错了,这里有一些重现我的问题的基本代码:

我的2个片段类:

package com.test.testbackfragment;

import android.support.v4.app.Fragment;
import android.view.ViewGroup;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

public class Fragment1 extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState)  {
        System.out.println("==== FRAGMENT1.ONCREATE");
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    
        System.out.println("==== FRAGMENT1.ONCREATEVIEW");
        View v = inflater.inflate(R.layout.fragment1, container, false);

        v.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    getActivity().getSupportFragmentManager().beginTransaction()
                        .remove(Fragment1.this)
                        .add(R.id.fragment, new Fragment2())
                        .addToBackStack(null)
                        .commit();
                }
            });
        return v;
    }        

    @Override
    public void onActivityCreated (Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        System.out.println("==== FRAGMENT1.ONACTIVITYCREATED (FRAGMENT1.getView = "+getView());
    }    
}

package com.test.testbackfragment;

import android.support.v4.app.Fragment;
import android.view.ViewGroup;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

public class Fragment2 extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState)  {
        System.out.println("==== FRAGMENT2.ONCREATE");
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    
        System.out.println("==== FRAGMENT2.ONCREATEVIEW");
        View v = inflater.inflate(R.layout.fragment2, container, false);
        return v;
    }        

    @Override
    public void onActivityCreated (Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        System.out.println("==== FRAGMENT2.ONACTIVITYCREATED");
    }    
}

主要活动非常简单

package com.test.testbackfragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

以下是3种布局: main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    <fragment android:name="com.test.testbackfragment.Fragment1"
            android:id="@+id/fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
</LinearLayout>

fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:background="#0000FF">
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="20dip"
            android:text="THIS IS FRAGMENT1" />

    <Button
            android:id="@+id/button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:textSize="20dip"
            android:text="FRAGMENT2"/>
</LinearLayout>

fragment2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:background="#00FF00">
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="20dip"
            android:text="THIS IS FRAGMENT2" />
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

当系统创建此main.xml布局时,它实例化布局中指定的片段并调用onCreateView()方法,以检索每个片段的布局。稍后它将使用相同的片段实例化。因此,它不会再次调用OnCreateView。

如果您想再次创建,请从您的Activity中获取FragmentTransaction的实例,并根据需要实现。

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction .replace(R.id.fragment, newFragment);
// Commit the transaction
transaction.commit();

答案 1 :(得分:0)

尝试使用FragmentManagerFragmentTransaction.remove(your fragment)堆栈中删除片段 BackPress事件之前的FragmentTransaction.replace()

或只需使用{{1}}加载新片段。