首页 > 移动平台 > 详细

Android_在Fragment获取activity实现的接口以及通过bundle传递自定义对象

时间:2020-03-09 18:18:25      阅读:113      评论:0      收藏:0      [点我收藏+]

要获取activity中实现的接口,可以在Fragment里重写onAttach方法,如下:

 public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (context instanceof ShowItemAdapter.ShowDetail){
            this.showDetail= (ShowItemAdapter.ShowDetail) context;
        }else {
            throw new RuntimeException(context.toString()+"havent impl interface");
        }
    }

  其中ShowDetail是context这个activity中实现的Show Item Adapter中的接口;

 

要将自己定义的Bean等对象在activity中传递,可以在这些对象类中实现 Parcelable 接口或是 Serializable 接口

如下:

public class ItemBean extends BmobObject implements Serializable {
    private String describe, tag, type, posi, username, bigtype,money;
    private String img, img_2, img_3;

    public String getMoney() {
        return money;
    }

    public void setMoney(String money) {
        this.money = "¥"+money;
    }
}

  

 需要传递的时候直接用bundle.putPracelable或是putSerializable

 

 

 

Android_在Fragment获取activity实现的接口以及通过bundle传递自定义对象

原文:https://www.cnblogs.com/XiaoGao128/p/12449753.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!