FragmentTransaction没有做任何事情 [英] FragmentTransaction not doing anything

查看:166
本文介绍了FragmentTransaction没有做任何事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am learning fragments and below given is my first fragment program. A simple project where I have 2 screens. When I click the next button of first screen, second button needs to be shown.

我瞄准了Android 2.1及以上和使用兼容包

I am targeting Android 2.1 and above and using compatibility package

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

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>

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

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>

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

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>

嗯,我得到的第一个屏幕没有问题。现在,

Well I am getting the first screen alright. Now,

  • 当我点击屏幕1下一步按钮,SecondFragment创建, 其的onCreate() onCreateView()被调用。
  • SecondFragment显示,并FirstFragment被销毁(因为我 不将其添加到backstack)。不会有因为任何动画 默认片段交易不具有动画效果。
  • When I click the Next button in screen 1, SecondFragment is created, its onCreate() and onCreateView() gets called.
  • SecondFragment is shown, and FirstFragment gets destroyed (since I am not adding it to backstack). There won't be any animation since default fragment transaction doesn't have animation.
  • SecondFragment是越来越创建好了,它的的onCreate() onCreateView()被调用。
  • 但是FirstFragment保留在屏幕上,而第二个从来没有显示。
  • SecondFragment is getting created alright, its onCreate() and onCreateView() gets called.
  • But FirstFragment remains on the screen, and second one never showing.

现在我的片段的理解可能是错的。但我相信,当我们提交()片段的交易,第一个片段应该由第二个取代(第一个无论是被隐藏或销毁)。那么似乎没有任何发生。这是为什么?我们应该手动销毁/隐藏第一个片段?

Now my understanding of fragment can be wrong. But I believe when we commit() a fragment transaction, first fragment should be replaced by second one (first one either gets hidden or destroyed). Well nothing seems to be happening. Why is that? Should we manually destroy/hide first fragment?

注:我知道这是一个长期的问题,对于这​​样一个基本的东西。但是,我把我的整个code,因为我不知道,我把事情搞糟了。

Note : I know it is a long question for such a basic thing. But I put my entire code since I am not sure where I messed it up.

推荐答案

嗯,我懂了工作。给这个问题的答案都正确地讲述了一个原因,我的code预期到不行。但是,在我的code 2的错误。

Well I got it working. Both answers given to this questions correctly tells one reason for my code to not work as expected. But there were 2 errors in my code.

使用 fragmentTransaction.add()不会出现新的片段在旧的。你需要调用替换()。给出的答案是正确的。

using fragmentTransaction.add() won't show new fragment over old one. you need to call replace(). The answers given were right.

不过,在我的code多了一个错误。你不能代替XML静态创建一个片段。所以我改变了

But there was one more error in my code. You can't replace a fragment created statically in xml. So I changed

<?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>

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

这篇关于FragmentTransaction没有做任何事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆