编辑:经过一些实验,如果我不在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
Run Code Online (Sandbox Code Playgroud)
如果我做错了什么,这里有一些重现我的问题的基本代码:
我的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 …Run Code Online (Sandbox Code Playgroud)