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

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/voidreturn/article/details/78302771 背景...
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/voidreturn/article/details/78302771

背景:在一次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
      }
  }
  ...
目录
相关文章
|
6月前
|
XML Android开发 数据格式
Android -- Fragment动态注册
Android -- Fragment动态注册
31 0
|
3月前
|
Android开发
Android基础知识:什么是Fragment?与Activity的区别是什么?
Android基础知识:什么是Fragment?与Activity的区别是什么?
271 54
|
4月前
|
Android开发 Kotlin
android开发,使用kotlin学习Fragment
android开发,使用kotlin学习Fragment
47 0
|
4月前
|
XML Java Android开发
Android Studio App开发之碎片Fragment的讲解及实战(附源码 包括静态和动态注册)
Android Studio App开发之碎片Fragment的讲解及实战(附源码 包括静态和动态注册)
40 1
|
6月前
|
XML Java 开发工具
Android lint配置及使用
Android lint配置及使用
|
7月前
|
Android开发
Android应用开发权限
Android应用开发权限
44 1
|
8月前
|
Android开发
Android ViewModel+LiveData实现Fragment间通信详解
Android ViewModel+LiveData实现Fragment间通信详解
101 0
|
8月前
|
Android开发
Android 中Activity和Fragment生命周期的具体变化详解
Android 中Activity和Fragment生命周期的具体变化详解
90 0
|
8月前
|
Android开发
Android 中Fragment和Activity之间的通信
Android 中Fragment和Activity之间的通信
42 0