Android应用开发—重载fragment构造函数导致的lint errors

背景:在一次release打包中发现lint报以下错误:
Error: Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead [ValidFragment]
根据后面的log提示是由于重载了fragment的构造函数,虽然紧接着lint提供了一个修改build script解决该问题的方法,不过这种方法对于有“追求”的程序员,是不会这么解决掉的。如何解决呢,google以下吧。

Avoid non-default constructors in fragments: use a default constructor plus Fragment报错的解决方法
以上链接文章其实已经给出了解决方法,没其他需要补充的了。

但是为什么需要这样做,上文没有提到,我只猜测下了:
文中提到这么个原因:

其原因是你重载了fragment的构造方法,但是在一些情况下,如屏幕翻转时,fragment被重新创建,就可能会造成数据丢失。

我的理解是,当fragment重建时,重载的构造方法的参数会丢失。但为什么通过newInstance就不会丢失呢?

    public static CustomCommonEditDialog newInstance(String content, int type) {
        CustomCommonEditDialog newFragment = new CustomCommonEditDialog();
        Bundle bundle = new Bundle();
        bundle.putString("content", content);
        bundle.putInt("type", type);
        newFragment.setArguments(bundle);
        return newFragment;
    } 

我猜测是bundle的数据是在fragment重建过程中是不会丢失的。(各位有经验的android开发者不要嘲笑我,不太了解fragment.setArguments这个接口的作用,对于一个只有三个月开发经验的android程序员,还有太多东西要了解,这个先放一放了)

补充用到的代码:

//在fragment onCreate接口中获取上面的参数
Bundle args = getArguments();
if (args != null) {
    content = args.getString("content");
    type = args.getInt("type");
}

调用:

CustomCommonEditDialog dialog = CustomCommonEditDialog.newInstance("String", 1);

附错误log(log里其实已经写的很明确了,E文很重要啊):

CustomCommonEditDialog.java:47: Error: Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead [ValidFragment]
    public CustomCommonEditDialog(String content, int type) {
           ~~~~~~~~~~~~~~~~~~~~~~

   Explanation for issues of type "ValidFragment":
   From the Fragment documentation:
   Every fragment must have an empty constructor, so it can be instantiated
   when restoring its activity's state. It is strongly recommended that
   subclasses do not have other constructors with parameters, since these
   constructors will not be called when the fragment is re-instantiated;
   instead, arguments can be supplied by the caller with setArguments(Bundle)
   and later retrieved by the Fragment with getArguments().

   http://developer.android.com/reference/android/app/Fragment.html#Fragment()

1 errors, 0 warnings
 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':vassistant:lintVitalRelease'.
> Lint found fatal   while assembling a release target.

  To proceed, either fix the issues identified by lint, or modify your build script as follows:
  ...
  android {
      lintOptions {
          checkReleaseBuilds false
          // Or, if you prefer, you can continue to check for errors in release builds,
          // but continue the build even when errors are found:
          abortOnError false
      }
  }
  ...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值