日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發(fā)布時間:2023/12/19 Android 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android应用开发—重载fragment构造函数导致的lint errors 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

背景:在一次release打包中發(fā)現(xiàn)lint報以下錯誤:
Error: Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead [ValidFragment]
根據(jù)后面的log提示是由于重載了fragment的構造函數(shù),雖然緊接著lint提供了一個修改build script解決該問題的方法,不過這種方法對于有“追求”的程序員,是不會這么解決掉的。如何解決呢,google以下吧。

Avoid non-default constructors in fragments: use a default constructor plus Fragment報錯的解決方法
以上鏈接文章其實已經(jīng)給出了解決方法,沒其他需要補充的了。

但是為什么需要這樣做,上文沒有提到,我只猜測下了:
文中提到這么個原因:

其原因是你重載了fragment的構造方法,但是在一些情況下,如屏幕翻轉時,fragment被重新創(chuàng)建,就可能會造成數(shù)據(jù)丟失。

我的理解是,當fragment重建時,重載的構造方法的參數(shù)會丟失。但為什么通過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的數(shù)據(jù)是在fragment重建過程中是不會丟失的。(各位有經(jīng)驗的android開發(fā)者不要嘲笑我,不太了解fragment.setArguments這個接口的作用,對于一個只有三個月開發(fā)經(jīng)驗的android程序員,還有太多東西要了解,這個先放一放了)

補充用到的代碼:

//在fragment onCreate接口中獲取上面的參數(shù) Bundle args = getArguments(); if (args != null) {content = args.getString("content");type = args.getInt("type"); }

調用:

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

附錯誤log(log里其實已經(jīng)寫的很明確了,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 instantiatedwhen restoring its activity's state. It is strongly recommended thatsubclasses do not have other constructors with parameters, since theseconstructors 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 warningsFAILEDFAILURE: 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}}...

總結

以上是生活随笔為你收集整理的Android应用开发—重载fragment构造函数导致的lint errors的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。