【Android组件核心面试题】Fragment的构造函数为啥不让传参?

Fragment的构造函数为啥不让传参?(B站)

这道题想考察什么?
  1. 是否了解Fragment传参的真实场景使用,是否熟悉Fragment使用场景?
考察的知识点
  1. Fragment参数处理在项目中使用与基本知识
考生应该如何回答
1.你在工作中,留意过在Android中Fragment构造函数传递参数的问题吗,你怎么看?

答:

在工作中,fragment不用构造函数传递参数

那么为什么fragment不能用构造函数传参数呢?

我们可以查看一下Fragment的源码

 public Fragment() {
     // ...
 }

在源码中会发现,fragment的构造函数是空的,所以他和普通类的创建对象的方式不太一样。

接着我们看源码:

 public static Fragment instantiate(Context context, String fname) {
 	return instantiate(context, fname, null);
 }
public static Fragment instantiate(Context context, String fname, Bundle args) {
     try {
        Class<?> clazz = sClassMap .get(fname);
        if (clazz == null) { // Class not found in the cache, see if it's real, and try to add it 
        clazz = context.getClassLoader().loadClass(fname);
        sClassMap .put(fname, clazz);
     }
    /*获取Bundle原先的值,这样一开始利用Bundle传递进来的值,就放入f. mArguments. 
    只需要在Fragment中利用getArguments().getString("key");就能将参数取出来继续用 */
    Fragment f = (Fragment)clazz.newInstance(); 
    if (args != null) {
        args.setClassLoader(f.getClass().getClassLoader());
        f. mArguments = args; 
    }
    return f;
 } catch (ClassNotFoundException e) {
     throw new InstantiationException( "Unable to instantiate fragment " + fname + ": make sure class name exists, is public, and has an" + " empty constructor that is public" , e);
 } catch (java.lang.InstantiationException e) {
    throw new InstantiationException( "Unable to instantiate fragment " + fname + ": make sure class name exists, is public, and has an" + " empty constructor that is public" , e); 
 } catch (IllegalAccessException e) { 
    throw new InstantiationException("Unable to instantiate fragment " + fname + ": make sure class name exists, is public, and has an" + " empty constructor that is public" , e); 
 } //...

是instantiate这个方法的作用是创建了fragment对象

再接着查看下instantiate:

			Class<?> clazz = sClassMap.get(fname);
            if (clazz == null) {
                // Class not found in the cache, see if it's real, and try to add it
                clazz = context.getClassLoader().loadClass(fname);
                if (!Fragment.class.isAssignableFrom(clazz)) {
                    throw new InstantiationException("Trying to instantiate a class " + fname
                            + " that is not a Fragment", new ClassCastException());
                }
                sClassMap.put(fname, clazz);
            }
            Fragment f = (Fragment)clazz.newInstance();
            if (args != null) {
                args.setClassLoader(f.getClass().getClassLoader());
                f.mArguments = args;
            }
            return f;

以上代码中我们发现Fragment是用反射的方式创建的,而且有mArguments来控制参数那么当然要用特定的方式来传递参数了

所以我们可以用bundle对象来传递参数:

Bundle bundle = new Bundle();
bundle.putSerializable("entity", entity);
fragment.setArguments(bundle);

最后

我整理了一套Android面试题合集,除了以上面试题,还包含【Java 基础、集合、多线程、虚拟机、反射、泛型、并发编程、Android四大组件、异步任务和消息机制、UI绘制、性能调优、SDN、第三方框架、设计模式、Kotlin、计算机网络、系统启动流程、Dart、Flutter、算法和数据结构、NDK、H.264、H.265.音频编解码、FFmpeg、OpenMax、OpenCV、OpenGL ES
在这里插入图片描述

有需要的朋友可以扫描下方二维码,免费领取全部面试题+答案解析!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值