{"id":254,"date":"2017-02-14T02:57:36","date_gmt":"2017-02-14T02:57:36","guid":{"rendered":"https:\/\/boochlin.com\/?p=254"},"modified":"2017-02-14T02:57:36","modified_gmt":"2017-02-14T02:57:36","slug":"android-issue-avoid-non-default-constructors-in-fragments","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=254","title":{"rendered":"Android issue &#8211; Avoid non-default constructors in fragments"},"content":{"rendered":"<h3>\u00a0<a href=\"https:\/\/boochlin.com\/wp-content\/uploads\/2017\/02\/image-16.png\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-258\" src=\"https:\/\/boochlin.com\/wp-content\/uploads\/2017\/02\/image-16.png\" alt=\"image-16\" width=\"600\" height=\"371\" \/><\/a><\/h3>\n<h3>\u672c\u7bc7\u91cd\u9ede<\/h3>\n<ol>\n<li><strong>\u6211\u9047\u5230\u4e86 non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead\u3002<\/strong><\/li>\n<li><strong>Parcelable \u5f88\u53b2\u5bb3\u3002<\/strong><\/li>\n<\/ol>\n<p>\u9019\u6b21\u958b\u767c side project \u7528\u5230\u5927\u91cf\u7684 fragment\u51fa\u65bc\u65b9\u4fbf\u7684\u60c5\u6cc1\u5c31\u4f7f\u7528\u5e95\u4e0b\u7684\u7528\u6cd5<\/p>\n<pre>public class MainPageTabFragment extends Fragment {\r\n    String tab;\r\n    User user;\r\n    public MainPageTabFragment(String tab , User user) {\r\n        this.tab = tab;\r\n        this.user = user;\r\n    }\r\n}\r\n\/\/ How to use\r\nMainPageTabFragment fragment = new MainPageTabFragment(tab, user);\r\n<\/pre>\n<p><!--more--><\/p>\n<p>\u9019\u5728\u7528 debug build \u6642\u4e26\u6c92\u6709\u9047\u5230\u4efb\u4f55\u554f\u984c\uff0c\u4f46\u662f\u5230\u8981\u7528 release build \u6642\u537b\u51fa\u73fe\u4e86 \u201c<span style=\"color: #ff0000;\">Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead<\/span>\u201d \u9019\u6a23\u7684\u63d0\u793a\u932f\u8aa4\uff0c\u5c0e\u81f4 release build \u4e0d\u904e\uff0c\u4e3b\u56e0\u662f\u7531\u65bc lint check code \uff0c\u4e26\u540c\u6642\u5efa\u8b70\u4f7f\u7528 setArgument() \u4f86\u53d6\u4ee3\u4f7f\u7528\u542b\u6709\u53c3\u6578\u7684\u5efa\u69cb\u5b50\u3002<\/p>\n<p>\u770b\u5230\u9019\u908a\u5982\u679c\u5c31\u53ea\u662f\u60f3\u55ae\u7d14\u89e3\u6c7a lint \u7684\u554f\u984c\uff0c\u90a3\u63d0\u4f9b\u6700\u7c21\u55ae\u7684\u65b9\u6cd5\uff5e\uff5e\uff5e\uff5e\uff5e\u90a3\u5c31\u4e0d\u8981\u6aa2\u67e5\u554a\uff0c\u53ea\u8981\u5728 build.gradle \u5167\u52a0\u5165\u5e95\u4e0b\u7684 code<\/p>\n<pre>android {\r\n          lintOptions {\r\n              checkReleaseBuilds false\r\n          }\r\n      }\r\n<\/pre>\n<p>\u6216\u662f\u5728 fragment \u52a0\u4e0a @SuppressLint(&#8220;ValidFragment&#8221;)<br \/>\nref:<a href=\"http:\/\/stackoverflow.com\/questions\/30621520\/how-to-ignore-the-avoid-non-default-constructors-in-fragments-error\">stackoverflow<\/a><\/p>\n<p>\u5230\u9019\u88e1\uff0c\u5c31\u89e3\u6c7a\u4e86 lint \u5c0e\u81f4 release build \u7684\u554f\u984c\uff0c\u5ba2\u500c\u60f3\u5148\u4ea4\u5dee\u7d66\u8001\u95c6\u7684\u8a71\uff0c\u9019\u908a\u53ef\u4ee5\u6536\u5de5\u5566\uff0c\u5982\u679c\u9084\u6709\u8208\u8da3\u7684\u8a71\u5c31\u7e7c\u7e8c\u4f86\u7814\u7a76\u70ba\u4f55 lint \u4e0d\u5efa\u8b70\u9019\u6a23\u641e\u3002<\/p>\n<p>\u4e3b\u8981\u539f\u56e0\u662f fragment \u7684\u7279\u6027<br \/>\n\u300eAll subclasses of Fragment must include a public no-argument constructor. The framework will often re-instantiate a fragment class when needed, in particular during state restore, and needs to be able to find this constructor to instantiate it. If the <span style=\"color: #ff0000;\">no-argument constructor is not available, a runtime exception will occur in some cases during state restore.<\/span>\u300f<\/p>\n<p><span style=\"color: #0000ff;\">\u7d50\u8ad6\u5c31\u662f\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff08\u87a2\u5e55\u65b9\u5411\u8f49\u63db\uff09\uff0cfragment \u88ab\u7cfb\u7d71\u91cd\u5efa\u6642\uff0c\u53ea\u6703\u547c\u53eb\u7121\u53c3\u6578\u7684\u5efa\u69cb\u5b50\uff0c\u4f60\u7279\u5730\u7528\u6709\u53c3\u6578\u7684\u5efa\u69cb\u5b50\u5b58\u8d77\u4f86\u7684\u53c3\u6578\u4e0d\u6703\u88ab\u5b58\u8d77\u4f86<\/span>\u3002\u770b\u5230\u9019\u908a\u5c31\u77e5\u9053\uff0c\u904e\u53bb\u7684\u7528\u6cd5\u591a\u5371\u96aa\uff08\u96d6\u7136\u6211\u9396\u5b9a\u87a2\u5e55\u65b9\u5411\u4e86\uff0c\u4f46\u9084\u662f\u5f88\u96e3\u4fdd\u8b49\u6709\u5565\u9b3c\u60c5\u6cc1\uff09\uff0c\u56e0\u6b64\u63a5\u4e0b\u4f86\u5c31\u4f86\u91cd\u69cb\u7a0b\u5f0f\u78bc<\/p>\n<pre>public static MainPageTabFragment newInstance(String tab, User user) {\r\n        MainPageTabFragment fragment = new MainPageTabFragment();\r\n        Bundle argument = new Bundle();\r\n        argument.putString(MainPageTabFragment.KEY_TAB, tab);\r\n        argument.putParcelable(MainPageTabFragment.KEY_LOGIN_USER, user);\r\n        fragment.setArguments(argument);\r\n        return fragment;\r\n    }\r\n\/\/ then get argument on onCreate() or onCreateView()\r\npublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {\r\n        Bundle argument = getArguments();\r\n        this.tab = argument.getString(KEY_TAB);\r\n        this.loginUser = (User) argument.getParcelable(KEY_LOGIN_USER);\r\n}\r\n\/\/ How to use\r\nMainPageTabFragment fragment = MainPageTabFragment.newInstance(tab,user);\r\n\r\n<\/pre>\n<p>\u770b\u5230\u9019\u908a\u767c\u73fe\u4e00\u4ef6\u7cdf\u7cd5\u7684\u4e8b\u60c5\uff0c\u5c31\u662f User \u4e26\u975e java \u57fa\u672c\u578b\u5225\uff0c\u6839\u672c\u7121\u6cd5\u653e\u9032 Bundle \u88e1\u9762\uff0c\u90a3\u5c31\u4e0d\u80fd setArgument()\uff0c\u7136\u5f8c\u6574\u5957\u91cd\u4f86\u3002<\/p>\n<p>\u4e8b\u60c5\u7576\u7136\u6c92\u6709\u90a3\u9ebc\u56b4\u91cd\uff0c\u628a User \u5e8f\u5217\u5316\u5c31\u53ef\u4ee5\u4e86\uff0c\u5e8f\u5217\u5316\u7684\u65b9\u6cd5\u4e0d\u5c11\uff08\u81ea\u5df1\u5beb\u90fd\u53ef\u4ee5\uff09\uff0c\u4f46\u76ee\u524d\u6700\u4e3b\u6d41\u7684\u6709\u4e09\u7a2e<br \/>\n<span style=\"color: #0000ff;\">1. Serializable<\/span><br \/>\n<span style=\"color: #0000ff;\"> 2. Parcelable<\/span><br \/>\n<span style=\"color: #0000ff;\"> 3. Gson<\/span><br \/>\n\u6b63\u7576\u982d\u75db\u8981\u7528\u54ea\u4e00\u5957\u6642\uff0c\u770b\u5230\u9019\u7bc7<a href=\"http:\/\/blog.prolificinteractive.com\/2014\/07\/18\/why-we-love-parcelable\/\">\u6587\u7ae0<\/a>\u9084\u6709\u9019\u5f35\u5716<br \/>\n<img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-258\" src=\"https:\/\/boochlin.com\/wp-content\/uploads\/2017\/02\/image-16.png\" alt=\"image-16\" width=\"600\" height=\"371\" \/><\/p>\n<p><span style=\"color: #0000ff;\">Parcelable \u8d85\u7d1a\u5feb<\/span>\uff0c\u5c31\u6c7a\u5b9a\u662f\u4f60\u4e86\u3002\uff08\u81f3\u65bc\u70ba\u4f55\u90a3\u9ebc\u5feb\uff0c\u53ef\u4ee5\u8a73\u7d30\u95b1\u8b80\u6587\u7ae0\uff0c\u4e3b\u8981\u662f gson and Serializable \u90fd\u6709\u9700\u8981\u578b\u5225\u6620\u5c04\u7684\u6642\u9593\uff09\u5b9a\u6848\u5f8c\u63a5\u8005\u5c31\u8981\u4f86\u5be6\u4f5c User \u7684 Parcelable interface\uff0c\u9019\u90e8\u5206\u4e3b\u8981\u6709\u5e7e\u500b\u6a21\u584a<\/p>\n<pre>    \/\/ \u8b1b\u4e00\u4e0b\u505a\u5565\r\n    @Override\r\n    public int describeContents() {\r\n        return 0;\r\n    }\r\n    \/\/ \u5beb\u5165\u5bb9\u5668 parcel\r\n    @Override\r\n    public void writeToParcel(Parcel dest, int flags) {\r\n        dest.writeValue(this.id);\r\n        dest.writeString(this.userName);\r\n        dest.writeString(this.email);\r\n        dest.writeString(this.userID);\r\n        dest.writeString(this.userCode);\r\n        dest.writeString(this.userIcon);\r\n    }\r\n    \/\/ \u50b3\u5165\u5bb9\u5668 parcel\r\n    protected User(Parcel in) {\r\n        this.id = (Long) in.readValue(Long.class.getClassLoader());\r\n        this.userName = in.readString();\r\n        this.email = in.readString();\r\n        this.userID = in.readString();\r\n        this.userCode = in.readString();\r\n        this.userIcon = in.readString();\r\n    }\r\n    \/\/ \u63d0\u4f9b\u9084\u539f\u65b9\u6cd5\r\n    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {\r\n        @Override\r\n        public User createFromParcel(Parcel source) {\r\n            return new User(source);\r\n        }\r\n\r\n        @Override\r\n        public User[] newArray(int size) {\r\n            return new User[size];\r\n        }\r\n    };\r\n<\/pre>\n<p>\u6700\u5f8c\u63d0\u4e00\u4e0b\uff0c\u4e0a\u9762\u7684 Parcelable \u6839\u672c\u5c31\u662f dirty code \uff0c\u6240\u4ee5\u76f4\u89ba\u4e00\u5b9a\u6709\u81ea\u52d5\u5316\u5de5\u5177\uff0c\u6240\u4ee5\u5206\u4eab<a href=\"https:\/\/github.com\/mcharmas\/android-parcelable-intellij-plugin\">\u00a0android parcelable code generator<\/a>\u00a0\u7d66\u5927\u5bb6\u3002<\/p>\n<p><a href=\"https:\/\/boochlin.com\/wp-content\/uploads\/2017\/02\/screenshot.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-257\" src=\"https:\/\/boochlin.com\/wp-content\/uploads\/2017\/02\/screenshot.png\" alt=\"screenshot\" width=\"600\" height=\"601\" \/><br \/>\n<\/a><\/p>\n<p>\u4e00\u500b CLICK \u5c31\u5168\u90e8\u81ea\u52d5\u751f\u51fa CODE\uff0c\u611f\u8b1d\u5404\u4f4d\u770b\u5230\u9019\u88e1\u3002\u6253\u70ca\u5566<\/p>\n<p>ref:<br \/>\n<a href=\"http:\/\/www.developerphil.com\/parcelable-vs-serializable\/\">http:\/\/www.developerphil.com\/parcelable-vs-serializable\/<\/a><br \/>\n<a href=\"http:\/\/blog.csdn.net\/kroclin\/article\/details\/40902721\">http:\/\/blog.csdn.net\/kroclin\/article\/details\/40902721<\/a><\/p>\n<p><a href=\"http:\/\/blog.prolificinteractive.com\/2014\/07\/18\/why-we-love-parcelable\/\">http:\/\/blog.prolificinteractive.com\/2014\/07\/18\/why-we-love-parcelable\/<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0 \u672c\u7bc7\u91cd\u9ede \u6211\u9047\u5230\u4e86 non-default constructors in fragments: use  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50,5,61],"tags":[6,66],"_links":{"self":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/254"}],"collection":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=254"}],"version-history":[{"count":0,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/254\/revisions"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}