android - How to Resolve This Fragment "non-default constructor" Error? -


i have inherited android app. when attempt build signed apk, error says error:(31) error: avoid non-default constructors in fragments: use default constructor plus fragment#setarguments(bundle) instead [validfragment]

it points bit of code:

public class basicinfofragment extends basepassportfragment implements locationedittext.locationedittextlistener {      private user user;     private templates templates;      public basicinfofragment() {         super(false);     }      public basicinfofragment(user user) {         super(false);         this.user = user;     }      public basicinfofragment(user user, templates templates) {         super(false);         this.user = user;         this.templates = templates;     }     ...  } 

the error points 2nd , 3rd methods, public basicinfofragment(user user) , public basicinfofragment(user user, templates templates) confused why public basicinfofragment() okay other 2 not?

i confused why public basicinfofragment() okay other 2 not?

when activity destroyed , recreated part of configuration change (e.g., screen rotation), android automatically destroys , recreates fragments managed old activity instance. that, use default constructor, not either of other 2 constructors.

also, when app's process terminated while in background, , user returns app (e.g., recent-tasks list), android create brand-new instance of activity... , of fragments. once again, use default constructor.

the risk of using other constructors objects passed in constructors prone being lost during either of above events. lint steering towards getting rid of constructors , using factory pattern (e.g., static newinstance() method), data supplied fragment via setarguments() method. attaches bundle, retrievable fragment instance via getarguments(). bundle automatically part of saved instance state, held onto through either of above events.

tl;dr: other public constructors represent strong enough "code smell" lint nowadays yells @ you


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -