fragment 与 activity

原文:http://blog.csdn.net/shanxuyang/article/details

activity向fragment传值

1.要传的值  放到Bundle对象里

2.通过创建传递封Fragment 将bundle对象 通过 setArguments()传递到fragment

3.Fragment中 通过getArguments() 得到 bundle对象  就能得到里面的值

Fragment向activity中传值

1.在Fragment中写一个回调接口  

2.在activity中实现这个回调接口

3,在Fragment中onAttach 方法中得到activity中实现好的 实例化接口对象

4,用接口的对象  进行传值

fragment 向 fragment传值

第一种

1.fragment通过回调接口 将值返给Activity

2.Activity 通过setArguments 传值给另一个Fragment

第二种

通过fragment指定的id 找到Fragment对象

 ————————————————————————————————————————

fragment是什么?

在Activity中使用的碎片或片段,有自己的布局、生命周期和输入事件即方法

fragment怎么用?

 1、创建类,并继承Fragment
 2、重写Fragment的onCreateView()生命周期方法,并返回一个View
 3、在布局文件中使用<fragment android:name="自定义Fragment的类路径" />

使用FragmentManager

            作用:管理多个Frament之间的交互和传值的
            android 3.0以前版本:当前Activity需要继承FragmentActivity并引入v4包
            Activity.getFragmentManager()  3.0以后
            FragmentActivity.getSupportFragmentManager() 3.0以前,引用v4包
            FragmentTransaction beginTransaction() 获取Fragment事务处理对象
使用FragmentTransaction
            作用:Fragment的处理事处类,可以在指定的ViewGroup上增加、更新、删除Fragment
            add(int containerViewId, Fragment fragment) 将Fragment对象增加到指定的ViewGroup控件上
            replace(int containerViewId, Fragment fragment, String tag) 替换指定ViewGroup上的Fragment

            commit() 提交本次事务处理

 11个生命周期方法
            onAttach(Activity)  连接宿主Activity
            onCreate(Bundle)  创建Fragment
            onCreateView(LayoutInflater, ViewGroup, Bundle)创建Fragment视图 
            onActivityCreated(Bundle) 当宿主Activity的onCreate()执行完之后调用
            onStart() 
            onResume() 
            onPause()
            onStop() 
            onDestroyView() 销毁Fragment视图,与onCreateView对应
            onDestroy()  销毁Fragment,与onCreate对应
            onDetach() 与宿主Activity断开连接,与onAttach对应
            同Activity的Created状态
            同Activity相应的生命周期方法
            同Activity的Destory

生命周期流程
            1、当Activity创建时,调用Fragment的onAttach->onCreate->onCreateView->onActivityCreated
            2、当Activity启动时,调用Fragment的onStart
            3、当Activity获取焦点时,调用Fragment的onResume
            4、当跳转到另一个Activity时,调用Fragment的onPause-->onStop
            5、当返回Activity时,调用Fragment的onStart->onResume

            6、销毁Activity时,调用Fragment的onDestroyView->onDestory->onDetach

fragment传值方式

     1.Bundle方式
            通过Fragment.setArguments(Bundle)传值,这种方式只适用还没有显示的Fragment
            在Fragment中,通过Fragment.getArguments()获取Bundle
        2.Activity的方式
            在Activity中获取Fragment中的UI控件,并增加相关事件
            在Activity声明公共方法,在Fragment中调用getActivity()并强转,则可以调用公共方法向其他Fragment控件传值

        3.接口回调方式

回退栈
        作用:因在同一ViewGroup中显示多个Fragment,因此需要回退到上一次的Fragment时,需要回退栈
        FragmentTransaction.addToBackStack(String name) 将当前的Fragment增加到回退栈中
    相关类
        ListFragment
            定义:带有ListVeiw控件的Fragment
            setListAdapter(BaseAdapter) 设置ListView显示内容的适配器
            重写onListItemClick()方法处理LIstView的item点击事件
        DialogFragment
            定义:带有生命周期的对话框
            创建类,并继承DialogFragment
            重写onCreateView方法,并通过setStyle()设置对话框样式
            重写onCreateDialog()方法,并返回Dialog
            在Activity中通过自定义类的对象调用show(FragmentManager fm, String tag)

————————————————————————————————————————————————————————————

在Activity中加载Fragment
Fragment的加载方式包含两种:静态加载和动态加载。静态加载很简单,我们只需要把Fragment(片段)当成普通UI控件放到界面Layout中就行;动态加载稍微复杂一点,需要用到事务。
 
静态加载
在Activity中静态加载Fragment的过程分为三步:
1. 创建一个Layout文件,就是我们的Fragment的UI界面
2. 创建一个类继承Fragment,然后重写里面的onCreateView方法,将Fragment的Layout变成View
3. 在Layout布局文件中声明fragment,android:name属性里是我们上面创建的类,另外,fragment必须用id或tag作为唯一标识
动态加载
实现动态加载,我们需要先了解Fragment事务。事务指的就是一种原子性、不可拆分的操作。
所谓的Fragment事务就是:对Fragment进行添加、移除、替换或执行其它动作,提交给Activity的每一个变化。
这就是Fragment事务。
 
Fragment是UI模块,自然在一个Activity中可以不只有一个模块,所以Android提供了FragmentManage类来管理Fragment,FragmentTransaction类来管理事务。我们对Fragment的动态加载就是先将添加、移除等操作提交到事务,然后通过FragmentManage完成的。
 
通过FragmentManager.beginTransaction()我们可以开始一个事务。在事务中,我们可以对Fragment进行的操作以及对应的方法如下:
添加:add()
移除:remove()
替换:replace()
提交事务:commit()
上面几个是比较常用的,还有attach()、detach()、hide()、addToBackStack()等方法。
我们需要注意的是,Fragment以ID或Tag作为唯一标识,所以remove和replace的参数是Fragment,这个Fragment目标Fragment一致。

 ——————————————————————————

Fragment与Activity通信的方式如下:

  一、通过初始化函数提供

    1.在动态添加Fragment的过程中,我们在Activity中通过Fragment.setArguments()的方法为Fragment提供数据;

     2.在Fragment中,在onAttach()函数中通过调用getArguments()获得一个Bundle对象,从而获取我们提供的数据。

  二、创建回调接口

    比如说:新闻浏览情境下,共有两个Fragment,一个是用来显示新闻标题;另外一个用来显示新闻内容。当我们点击新闻标题的时候,新闻的内容就显示到另外一个Fragment当中了。下面我们就来通过按下Fragment1中的button来传递一个消息到Fragment2中。

如果您看见了我的文章,希望得到您的支持,由衷感谢!
原文地址:https://www.cnblogs.com/h4ck/p/5404227.html