> 文档中心 > Android基础--Android Fragment的使用,以及“commit already called”问题解决

Android基础--Android Fragment的使用,以及“commit already called”问题解决

从Android3.0版本开始,Google为Android系统引入了fragment的概念。翻译成中文,意为碎片,即把原来在Activity上显示的布局控件,有效的分割,并显示再多个fragment中,而fragment依赖于Activity。这样,UI界面的设计将变得更加动态和灵活,复用性更强,很容易适配到诸如平板设备等更大的屏幕上,甚至在Android系统的电脑上。更重要的是,对于程序开发者来说,几乎可以做到无缝迁移,你只需要将activity的layout分成fragment即可。

本例是一个简单的fragment的使用示例。

1. 示例代码:

在一个Activity中包含Fragment,并且使用一个Button来控制它的显示。
在实践过程中,遇到不少问题,记录下来,并且简要说明下。

先看Activity:
FragmentActivity.java:

package com.example.fragmenttest0;import android.os.Bundle;import android.app.Activity;import android.app.Fragment;import android.app.FragmentTransaction;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class FragmentActivity extends Activity {    Button btn;    Fragment fragment;//fragment    FragmentTransaction ft;  //FragmentTransaction    TextView t;    boolean commit = false;    @Override    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment); ft = getFragmentManager().beginTransaction();         /*为Fragment设置淡入淡出效果,Android开发网提示这里这两个动画资源是android内部资源无需我们手动定义。*/          ft.setCustomAnimations(android.R.animator.fade_in,                  android.R.animator.fade_out);                fragment = getFragmentManager().findFragmentById(R.id.fragment0);        ft.add(fragment, "FragmentTest0000");        t = (TextView)findViewById(R.id.text); btn = (Button)findViewById(R.id.btn); MyclickListener l = new MyclickListener(); btn.setOnClickListener(l);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.                getMenuInflater().inflate(R.menu.fragment_test0, menu);        return true;    } public class MyclickListener implements OnClickListener {    @Override    public void onClick(View arg0) { // TODO Auto-generated method stub String str = getString(R.string.ok); System.out.println("btn.getText=" + btn.getText()); System.out.println("string=" + getString(R.string.ok)); if (btn.getText()==getString(R.string.ok)) {     btn.setText(R.string.cancel);     ft.hide(fragment);              t.setText("fragment hidden"); } else {     btn.setText(R.string.ok);     ft.show(fragment);              t.setText("fragment show"); } ft.commit();   // throw exception    }    }}

对应的布局文件:
activity_fragment.xml:

        

labeled_text_edit.xml:

                    

menu文件, 定义在fragment_test0.xml:

    

再看Fragment的定义:

用户定义的Fragment必须继承自Fragment。

FirstFragment.java:

package com.example.fragmenttest0;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class FirstFragment extends Fragment {    TextView mTextView;    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {    //从文件 example_fragment.xml 加载了一个layout         View v = inflater.inflate(R.layout.labeled_text_edit, container, true);                View tv = v.findViewById(R.id.msg);        ((TextView)tv).setText("The fragment saves and restores this text.");        mTextView = (TextView)v.findViewById(R.id.saved);        if (savedInstanceState != null) {            mTextView.setText(savedInstanceState.getCharSequence("text"));        }        System.out.println("onCreateView    000000000000000000000");        return v;    }    @Override    public void onSaveInstanceState(Bundle outState) {        super.onSaveInstanceState(outState);        outState.putCharSequence("text", mTextView.getText());    }        }

Fragment也需要布局文件,使用的是labeled_text_edit.xml,即:

//从文件 example_fragment.xml 加载了一个layout 
    View v = inflater.inflate(R.layout.labeled_text_edit, container, true);

2.  踩坑与填坑:

1)。  出现的问题:
 点击两次就会 抛出下面的异常:
10-24 13:13:37.910: E/AndroidRuntime(24611): FATAL EXCEPTION: main
10-24 13:13:37.910: E/AndroidRuntime(24611): java.lang.IllegalStateException: commit already called
10-24 13:13:37.910: E/AndroidRuntime(24611): at android.app.BackStackRecord.commitInternal(BackStackRecord.java:533)

说明:如上所说,commit不能被同一个FragmentTransaction调用多次,上面的代码正是违反了这条规定
将最后一句ft.commit();改为 下面的语句,就不会有异常了。
if (!commit) {
ft.commit();
commit = true;
}

2) 虽然不抛出异常,但是,fragment总是显示不出来
说明:用如下两种方式来解决:
(1) 利用FragmentTransaction 的add 和remove方式来实现,这样的话,每次都要重新调用FirstFragment的onCreateView函数

代码改为:

   。。。。。。 if (btn.getText()==getString(R.string.cancel)){ft = getFragmentManager().beginTransaction();        /*为Fragment设置淡入淡出效果,Android开发网提示这里这两个动画资源是android内部资源无需我们手动定义。*/         ft.setCustomAnimations(android.R.animator.fade_in,                 android.R.animator.fade_out);                     ft.add(fragment, "FragmentTest0000");       ft.commit();       btn.setText(R.string.ok);                t.setText("btn ok!");}else {ft = getFragmentManager().beginTransaction();        /*为Fragment设置淡入淡出效果,Android开发网提示这里这两个动画资源是android内部资源无需我们手动定义。*/         ft.setCustomAnimations(android.R.animator.fade_in,                 android.R.animator.fade_out);       ft.remove(fragment);           ft.commit();btn.setText(R.string.cancel);                t.setText("btn cancel!");}

(2) 不要把FragmentTransaction ft作为类的数据成员,而是作为本地变量来使用,代码如下:
            FragmentTransaction ft
= getFragmentManager().beginTransaction(); 
       /*为Fragment设置淡入淡出效果,Android开发网提示这里这两个动画资源是android内部资源无需我们手动定义。*/  
       

ft.setCustomAnimations(android.R.animator.fade_in,                 android.R.animator.fade_out);              if (fragment.isHidden()) {                  ft.show(fragment);                  t.setText("fragment show");            } else {                  ft.hide(fragment);                  t.setText("fragment hidden");            }        ft.commit();if (btn.getText()==getString(R.string.cancel)){btn.setText(R.string.ok);                t.setText("btn ok!");}else {btn.setText(R.string.cancel);                t.setText("btn cancel!");}

注意: 这种方式和上一种方式的区别在于:
   每次点击button,都是重新创建FragmentTransaction的实例ft,用这个新的实例去操作已经有的fragment,通过hide和show方法。很明显,这种方式不会重新调用FirstFragment的onCreateView函数。

 

这对于Fragment的生命周期管理是非常重要的。