android Fragment

Fragment在应用当中应当是一个模块化和可重用的组件,Fragment可以说是对于Activity的一种模块化的抽象。因为Fragment定义了他自己的布局,以及通过使用他自己的生命周期回调方法定义了他自己的行为,一个Fragment可以被多个Activity加载。一个Activity可以加载多个Fragment。


Fragment有自己的生命周期,且在Activity的生命周期里面,会受宿主Activity的生命周期影响
onCreateView()方法返回一个View
Fragment第一次绘制它的用户界面的时候,系统会调用此方法,为了绘制Fragment的UI,此方法必须返回一个View,若不显示UI,返回null即可。



结束当前Fragment返回上一个Fragment
getSupportFragmentManager().popBackStack();//suport.v4包
或getFragmentManager().popBackStack();


生命周期:

Fragment生命周期:
Created【onAttach()->onCreate()->onCreateView()->onActivityCreated()】
onAttach()方法:当Fragment被添加到Activity时会回调这个方法且只调用一次
onCreate()方法:当创建Fragment时会回调这个方法且只调用一次
onCreateView()方法:给Activity绑定一个布局;每次创建都会绘制Fragment的View组件时回调该方法
onActivityCreated()方法:当Fragment所在的Activity启动完成后调用
______________________________________
Started【onStart()】启动Fragment
______________________________________
Resumed【onResume()】恢复Fragment时会被回调,调用onStart()方法后一定会调用onResume()方法
______________________________________
Paused【onPause()】暂停Fragment
______________________________________
Stopped【onStop()】停止Fragment
______________________________________
Destroyed【onDestroyView()->onDestroy()->onDetach()】
onDestroyView()方法:销毁Fragment所包含的View组件时回调
onDestroy()方法:销毁Fragment时会被回调
onDetach()方法:Fragment从Activity中删除时会被回调,且只调用一次


1. 启动Fragment
onAttach()->onCreate()->onCreateView()->onActivityCreated()->onStart()->onResume()
2. 屏幕锁屏
onPause()->onStop()
3. 屏幕解锁
onStart()->onResume()
4. 切换其他Fragment
前一个Fragment:
onPause()->onStop()->onDestoryView()->onDestory()->onDetach()
后一个Fragment:
onAttach()->onCreate()->onCreateView()->onActivityCreated()->onStart()->onResume()
5. 回到桌面
onPause()->onStop()
6. 回到应用
onStart()->onResume()
7. 退出Fragment
onPause()->onStop()->onDestoryView()->onDestory()->onDetach()



加载方式:

一.静态加载 :

1.Activity-->Fragment发送数据:
(1)在Fragment中,定义一个变量X(要传递的值),设置get,set方法
(2)在Activity中,调用FragmentMangaer的findFragmentById(),得到MyFragment,调用变量X的set方法

2.Fragment-->Activity发送数据:

(1)Fragment可调用getActivity()方法获取它所在的Activity
(2)Activity可调用FragmentMangaer的findFragmentById()或findFragmentByTag()方法获取Fratment


二.动态加载:

Activity-->Fragment发送数据:
(1).在Activity中创建Bundle数据包,并调用Fragment的setArguments(Bundle bundle)方法将数据包传递过去;
(2)在Fragment中调用getARugment()获取数据包


Fragment-->Activity发送数据:
(1).在Fragment中自定义一个接口

(2).在宿主Activity中实现该接口
(3).在Fragment中重写onAttach(Activity activity)

(4).调用按口中的方法,把要发送的数据当参数发送;
(5).在Activity中实现aaa(){}方法


代码如 下 :

主activity


package com.example.myfragment;

import com.example.myfragment.MyFragment.myinterface;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Activity1 extends Activity implements myinterface {

	private TextView tv;
	private Button bt;
	private EditText et;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.xml_activity1);
		tv = (TextView) findViewById(R.id.text);
		bt = (Button) findViewById(R.id.button);
		et = (EditText) findViewById(R.id.edit);

		bt.setText("确认");

		bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String ttt = et.getText().toString();
				Log.e("", ttt);
				MyFragment fragment = new MyFragment();// 创建一个fragment对象
				Bundle bundle = new Bundle();
				bundle.putString("name", ttt);
				fragment.setArguments(bundle);

				FragmentManager manager = getFragmentManager();// 获取Fragment管理者
				FragmentTransaction begin = manager.beginTransaction();// 开启一个事务
				begin.add(R.id.layout_2, fragment, "activity..");// 添加一个fragment对象(fragment)到指定的布局中(R.id.layout_2)
				begin.addToBackStack(null);// 设置返回键——返回到fragment前一个状态
				begin.commit();// 把事务提交给Activity

			}
		});

	}

	@Override
	public void aaa(String aaa) {
		tv.setText("我是actibity" + aaa);

	}

}


Fragment 代码:

package com.example.myfragment;

import android.app.Activity;
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 MyFragment extends Fragment {

	private TextView textF;
	private String aaa = "我是fragment传来的";
	private myinterface lick;
	
	interface myinterface {
		public void aaa (String aaa);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		/**
		* resource:Fragment需要加载的布局文件
		* root:加载layout的父ViewGroup
		* attactToRoot:false,不返回父ViewGroup
		*/
		
		View view = inflater.inflate(R.layout.fragment, container, false);
		textF = (TextView) view.findViewById(R.id.text_f);
		String fff = getArguments().getString("name");
		textF.setText("我是fragment" + fff);
		lick.aaa(aaa);
		return view;
	}

	@Override
	public void onAttach(Activity activity) {
		lick = (myinterface)activity;
		super.onAttach(activity);
	}
}

xml:

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

    <LinearLayout
        android:id="@+id/layout_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

java.lang.IllegalStateException: commit already called

该错误,是因为你的ft事务是全局的变量,只能commit一次。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值