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:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".FragmentActivity" >


    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cancel" />
    
    <fragment 
        android:name="com.example.fragmenttest0.FirstFragment"  
                android:id="@+id/fragment0" android:layout_weight="1"  
                android:layout_width="0px" android:layout_height="wrap_content" />

</LinearLayout>

labeled_text_edit.xml:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:padding="4dip"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content">


    <TextView android:id="@+id/msg"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingBottom="4dip" />


    <EditText android:id="@+id/saved"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/initial_text"
        android:freezesText="true">
        <requestFocus />
    </EditText>


</LinearLayout>

menu文件, 定义在fragment_test0.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >


    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>


</menu>


再看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的生命周期管理是非常重要的。

 


 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liranke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值