FragmentTransaction没有做任何事情

Kri*_*dra 9 android fragment fragmenttransaction

我正在学习片段,下面给出的是我的第一个片段程序.一个简单的项目,我有2个屏幕.当我点击第一个屏幕的下一个按钮时,需要显示第二个按钮.

在此输入图像描述

我的目标是Android 2.1及更高版本并使用兼容包

AppMainFragmentActivity.java

public class AppMainFragmentActivity extends FragmentActivity {
  @Override
  protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);
    setContentView(R.layout.app_main_layout);
  }
}
Run Code Online (Sandbox Code Playgroud)

app_main_layout.xml

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

   <fragment class="com.research.fragmentstudy.FirstFragment"     
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" 
      android:id="@+id/id_first_fragment"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

FirstFragment.java

public class FirstFragment extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.first_fragment_layout
                 , container,false);

     Button nextButton =(Button) view.findViewById(R.id.button);
     nextButton.setOnClickListener(nextListener);
     return view;
  }

  private OnClickListener nextListener  =   new OnClickListener() {
     @Override
     public void onClick(View v) {
        FragmentManager fm = ((AppMainFragmentActivity)FirstFragment.this
                           .getActivity()).getSupportFragmentManager();
        SecondFragment fragment = new SecondFragment();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fragment_container, fragment);
        ft.commit();
     }
  };
}
Run Code Online (Sandbox Code Playgroud)

first_fragment_layout.xml

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

   <TextView android:layout_height="wrap_content" 
       android:layout_width="fill_parent"
       android:text="Fragment 1" android:gravity="center_horizontal" />

   <Button android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal"
       android:layout_width="wrap_content" android:id="@+id/button"
       android:text="Next" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

SecondFragment.java

public class SecondFragment extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.second_fragment_layout,
                     container,false);
      return view;
   }
}
Run Code Online (Sandbox Code Playgroud)

second_fragment_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" android:layout_height="fill_parent"
   android:orientation="vertical" android:id="@+id/first_fragment_root">

   <TextView android:layout_height="wrap_content" 
      android:layout_width="fill_parent"
      android:text="Fragment 2" android:gravity="center_horizontal" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

好吧,我正在获得第一个屏幕.现在,

根据我对片段的理解,我的期望

  • 当我单击屏幕1中的Next按钮时,会创建SecondFragment,onCreate()onCreateView()调用它.
  • 显示了SecondFragment,并且FirstFragment被销毁(因为我没有将它添加到backstack).由于默认片段事务没有动画,因此不会有任何动画.

怎么了

  • SecondFragment是越来越创建好了,它onCreate()onCreateView()被调用.
  • 但是FirstFragment仍然在屏幕上,第二个从未显示.

现在我对片段的理解可能是错误的.但我相信当我们提交()一个片段事务时,第一个片段应该被第二个片段替换(第一个片段被隐藏或销毁).好吧似乎没有发生任何事情.这是为什么?我们应该手动销毁/隐藏第一个片段吗?

注意:我知道对于这样一个基本的事情来说这是一个很长的问题.但我把我的整个代码都放了,因为我不确定我把它搞砸了.

Kri*_*dra 12

好吧,我让它工作.这两个问题的答案都正确地说明了我的代码无法按预期工作的一个原因.但是我的代码中有2个错误.

使用fragmentTransaction.add()不会显示旧片段的新片段.你需要调用replace().给出的答案是正确的.

但是我的代码中又出现了一个错误.您无法替换在xml中静态创建的片段.所以我改变了

app_main_layout.xml

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

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

AppMainFragmentActivity.java

public class AppMainFragmentActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.app_main_layout);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FirstFragment fragment = new FirstFragment();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragment_container,fragment);
        fragmentTransaction.commit();           
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 6

您是否尝试过使用ft.replace(R.id.fragment_container,fragment)而不是添加?添加不会删除现有的片段.