Activity与Fragment通信

前言

在Android3.0的时候被引入,它的出现主要是给大屏幕设备提供更加灵活的UI支持。通过对Activity布局进行分片,更加方便的对每块进行独立控制。这些片段可以被不同的activity复用。


Fragment生命周期

Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期。官网这张图很好的说明了两者生命周期的关系:

这里写图片描述


可以看到Fragment比Activity多了几个额外的生命周期回调方法:

1.onAttach(Activity):
当Fragment与Activity发生关联时调用。

2.onCreateView(LayoutInflater, ViewGroup,Bundle)
创建该Fragment的视图

3.onActivityCreated(Bundle)
当Activity的onCreate方法返回时调用

4.onDestoryView()
与onCreateView相对应,当该Fragment的视图被移除时调用

5.onDestory
销毁fragment时回调

6.onDetach()
与onAttach相对应,当Fragment与Activity关联被取消时调用

tips:除了onCreateView,其他的所有方法如果你重写了,必须调用父类对于该方法的实现.

Activity如何向Fragment传值?

(1)在Fragment 的getInstance()方法中setArguments()设置参数

public static Fragment getInstance(String message){
        Bundle bundle = new Bundle();
        bundle.putString("msg",message);
        MainFragment fragment=new MainFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

(2)在MainFragment中通过getArguments()方法获取传值

public void getData() {
        Bundle bundle=getArguments();
        if(bundle!=null){
          String msg=bundle.getString("msg");
          Log.e("从activity中获取值为:",msg);  
        }
    }

Fragment如何向Activity传值

一般来说有两种方案,第一种是通过接口回调的方式,另外一种是通过广播的形式,下面对这两种方式做介绍:

1.接口回调方式

在fragment类中定义一个接口并在Activity中实现它。Fragment在onAttach(Activity activity)回调函数中获取接口的具体实现的对象。进而fragment就可以调用接口中的方法实现与Activity的通信。

public interface callBack{
        void showMsg(String message);
    }

    private callBack mCallBack;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            if(activity!=null){
                mCallBack=(MainActivity)activity;
            }
        } catch (ClassCastException e) {
            throw new ClassCastException(
                  " activity must implement callBack");
        }
    }


            public void onClick(View view) {
                mCallBack.showMsg("回调activity中的方法");
            }
        });

在Activity中需要实现callBack接口

@Override
    public void showMsg(String message) {
    if(!TextUtils.isEmpty(message)){
     Log.e("得到Fragment的传值为:",message);
    }
            }

2.广播的方式

在Activity中注册广播接收器,在Fragment中发送广播,进而实现Fragment向activity传值.

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ACTION_NAME)) {
                String msg = intent.getStringExtra("msg");
                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
            }

        }
    };

    public void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTION_NAME);
        registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
    }

实际开发中推荐使用接口回传的方式,不建议使用广播的方式。
根据广播的注册方式可以分为两种:

第一种是常驻的注册方式,即,当程序关闭后,若有消息传来,一样会自动进行响应。
第二种不是常驻的注册方式,及会随着Activity的生命周期的变化而变化。上面使用的就是这种方式

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值