inheritance - How to extend android class which implements Parcelable interface? -


first of have check this answer.

what trying extending location class calling locationplus has member variables. functionality trying achieve pass object of locationplus class 1 activity another.

here creator

public static final parcelable.creator<locationplus> creator = new parcelable.creator<locationplus>() {     @override      public locationplus createfromparcel(parcel source) {         return new locationplus(source);     }     @override      public locationplus[] newarray(int size) {         return new locationplus[size];     } }; 

problem facing error

implicit super constructor location() undefined. must explicitly invoke constructor 

when trying write constructor

public locationplus(parcel in) { 

someone in comment ask me post locationplus class here is

public class locationplus extends location{      private int mbattery = -1;      public locationplus(string locationname) {         super(locationname);     }      public locationplus(location location) {         super(location);     }      public int getmbattery() {         return mbattery;     }      public void setmbattery(int mbattery) {         this.mbattery = mbattery;     }     @override     public int describecontents() {         return 0;     }      public static final parcelable.creator<locationplus> creator = new parcelable.creator<locationplus>() {         @override          public locationplus createfromparcel(parcel source) {             return new locationplus(source);         }         @override          public locationplus[] newarray(int size) {             return new locationplus[size];         }     };      @override     public void writetoparcel(parcel out, int flags) {         super.writetoparcel(out, flags);         out.writeint(mbattery);     }      public locationplus(parcel in) {         mbattery =in.readint();     } } 

parcelable, speed king

according google engineers, code run faster. 1 of reasons being explicit serialization process instead of using reflection infer it. stands reason code has been heavily optimized purpose.

public abstract class baseclass implements parcelable {      public string fullname;     public boolean isvaliduser;     public string username;       public baseclass () {     }       protected baseclass(parcel in) {         fullname = in.readstring();         isvaliduser = in.readbyte() != 0;         username = in.readstring();     }      @override     public void writetoparcel(parcel dest, int flags) {         dest.writestring(fullname);         dest.writebyte((byte) (isvaliduser ? 1 : 0));         dest.writestring(username);     } } 

child class follows usage of list adding parcelable object:

public class derivedclass extends baseclass {      public boolean issuccess;     public string message;     public list<anotherclass> anotherclassobj;       public derivedclass () {         super();     }      protected derivedclass(parcel in) {         super(in);         anotherclassobj = new arraylist<anotherclass>();         issuccess = in.readbyte() != 0;         message = in.readstring();         anotherclassobj = in.readarraylist(anotherclass.class.getclassloader());     }      public static final creator<derivedclass> creator = new creator<derivedclass>() {         @override         public derivedclass createfromparcel(parcel in) {             return new derivedclass(in);         }          @override         public derivedclass[] newarray(int size) {             return new derivedclass[size];         }     };      @override     public void writetoparcel(parcel dest, int flags) {         super.writetoparcel(dest, flags);         dest.writebyte((byte) (issuccess ? 1 : 0));         dest.writestring(message);         dest.writelist(anotherclassobj);     }      public int describecontents() {         return 0;     } } 

another child class :

public class anotherclass extends baseclass {     public anotherclass() {         super();     }      protected anotherclass(parcel in) {         super(in);     }      public int describecontents() {         return 0;     }      public static final creator<anotherclass> creator = new creator<anotherclass>() {         @override         public anotherclass createfromparcel(parcel in) {             return new anotherclass(in);         }          @override         public anotherclass[] newarray(int size) {             return new anotherclass[size];         }     };      @override     public void writetoparcel(parcel dest, int flags) {         super.writetoparcel(dest, flags);     } } 

in activity:

 intent intent = new intent(loginactivity.this, mainactivity.class);  intent.putextra("userobject", parcelableobject);  startactivity(intent);  finish(); 

in receiving activity:

bundle extras = getintent().getextras();  if (extras != null) {       userobject = extras.getparcelable("userobject");  } 

Comments

Popular posts from this blog

javascript - JS causing window size to be bigger than necessary - Dropdown bug -

How to mention the localhost in android -

php - Calling a template part from a post -