ParcelableException: ClassNotFoundException when unmarshalling - android

I am sending data from one activity to another activity from different packages.I applied intent to send and receive data.
For sending single value its working but now I want to send object via intent so I implemented Parcelable but I am getting error that class not found.
Here is my code.....
Sender side Classes :-
Sender MainActivity
public class MainActivity extends AppCompatActivity {
Button click;
Student student = new Student(01,"Anjali");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click = (Button)findViewById(R.id.app1);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent act = new Intent("com.gov.printer");
act.putExtra("data",student);
startActivity(act);
}
});
}
}
Student Sender class
public class Student implements Parcelable {
int id;
String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static Creator<Student> getCREATOR() {
return CREATOR;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(name);
}
public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>()
{
#Override
public Student createFromParcel(Parcel source) {
return new Student(source);
}
#Override
public Student[] newArray(int size) {
return new Student[size]; }
};
private Student(Parcel in){
}
}
Receiver side Classes:-
Student receiver class
public class Student implements Parcelable{
int id;
String name;
public int get Id() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static Creator<Student> getCREATOR() {
return CREATOR;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
}
public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>()
{
#Override
public Student createFromParcel(Parcel source) {
return new Student(source);
}
#Override
public Student[] newArray(int size) {
return new Student[size];
}
};
private Student(Parcel in){
this.name = in.readString();
this.id = in.readInt();
}
}
Receiver MainActivity
public class MainActivity extends AppCompatActivity {
TextView getdata;
String TAG ="Main";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getdata = (TextView)findViewById(R.id.textapp);
Intent in = getIntent();
if (in.getAction().equals("com.gov.printer"));
{
Student student = in.getParcelableExtra("data");
Log.e(TAG,"data is"+student);
getdata.setText("name:-" +student.getName()+"\n"
+"ID:- " +student.getId());
}
}

You can achieve like this
Student class:
package com.ex.utils;
import android.os.Parcel;
import android.os.Parcelable;
public class Student implements Parcelable {
int id;
String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeString(this.name);
}
public Student() {
}
protected Student(Parcel in) {
this.id = in.readInt();
this.name = in.readString();
}
public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
#Override
public Student createFromParcel(Parcel source) {
return new Student(source);
}
#Override
public Student[] newArray(int size) {
return new Student[size];
}
};
}
Sender:
Intent i = new Intent("");
i.putExtra("data", student);
Receiver:
Student student = getIntent().getParcelableExtra("data");

Related

How to pass parcelable arg from activity to fragment with Android Annotations?

I have class ITEM and I want to pass array (Item[] items) of this class from ACTIVITY to FRAGMENT. Activity and fragment use Android Annotations.
public class Item implements Parcelable {
public int id;
public String code;
public Item() {
}
public Item(int id, String code) {
this.id = id;
this.code = code;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
protected Item(Parcel in) {
id = in.readInt();
code = in.readString();
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(code);
}
#SuppressWarnings("unused")
public static final Creator<Item> CREATOR = new Creator<Item>() {
#Override
public Item createFromParcel(Parcel in) {
return new Item(in);
}
#Override
public Item[] newArray(int size) {
return new Item[size];
}
};
}
Now I do it old way - I do not use parcelable, because I do not have to, but it seems that I canĀ“t pass same argument to fragment with Android Annotations (I may be wrong).
Activity:
new ItemFragment(items)
Fragment:
public class ItemFragment extends Fragment {
public ItemFragment (Item[] items) {
this.items= items;
}
}

Pass ArrayList of Parcelable objects throws error

I have an object with a couple of Strings, one int and another object which has 4 inner objects. All of them implementing Parcelable. In order to generate the boilerplate code, I used Parcelable plugin from Android studio.
Even though all the objects are parcelable, I'm getting the following error: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: AlbumTrackResponse
Here's how the POJOs look like:
Main object:
public class AlbumTrackResponse implements Parcelable {
private String id;
private String albumId;
private String position;
private String title;
private int rate;
private AllServices services;
public AllServices getServices() {
return services;
}
public void setServices(AllServices services) {
this.services = services;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAlbumId() {
return albumId;
}
public void setAlbumId(String albumId) {
this.albumId = albumId;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getRate() {
return rate;
}
public void setRate(int rate) {
this.rate = rate;
}
protected AlbumTrackResponse(Parcel in) {
id = in.readString();
albumId = in.readString();
position = in.readString();
title = in.readString();
rate = in.readInt();
services = (AllServices) in.readValue(AllServices.class.getClassLoader());
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(albumId);
dest.writeString(position);
dest.writeString(title);
dest.writeInt(rate);
dest.writeValue(services);
}
#SuppressWarnings("unused")
public static final Parcelable.Creator<AlbumTrackResponse> CREATOR = new Parcelable.Creator<AlbumTrackResponse>() {
#Override
public AlbumTrackResponse createFromParcel(Parcel in) {
return new AlbumTrackResponse(in);
}
#Override
public AlbumTrackResponse[] newArray(int size) {
return new AlbumTrackResponse[size];
}
};
}
Inner object with 4 objects
public class AllServices implements Parcelable {
private GigRevService gigrev;
private AppleService apple;
private GoogleService google;
private SpotifyService spotify;
protected AllServices(Parcel in) {
gigrev = (GigRevService) in.readValue(GigRevService.class.getClassLoader());
apple = (AppleService) in.readValue(AppleService.class.getClassLoader());
google = (GoogleService) in.readValue(GoogleService.class.getClassLoader());
spotify = (SpotifyService) in.readValue(SpotifyService.class.getClassLoader());
}
#Override
public int describeContents() {
return 0;
}
public GigRevService getGigrev() {
return gigrev;
}
public void setGigrev(GigRevService gigrev) {
this.gigrev = gigrev;
}
public AppleService getApple() {
return apple;
}
public void setApple(AppleService apple) {
this.apple = apple;
}
public GoogleService getGoogle() {
return google;
}
public void setGoogle(GoogleService google) {
this.google = google;
}
public SpotifyService getSpotify() {
return spotify;
}
public void setSpotify(SpotifyService spotify) {
this.spotify = spotify;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(gigrev);
dest.writeValue(apple);
dest.writeValue(google);
dest.writeValue(spotify);
}
#SuppressWarnings("unused")
public static final Parcelable.Creator<AllServices> CREATOR = new Parcelable.Creator<AllServices>() {
#Override
public AllServices createFromParcel(Parcel in) {
return new AllServices(in);
}
#Override
public AllServices[] newArray(int size) {
return new AllServices[size];
}
};
}
The inner objects from here contain just string items.
Here's how I pass the ArrayList to the bundle:
bundle.putParcelableArrayList(MediaPlayerService.TRACK_LIST, trackList);
I'm passing the items from an Activity to a Service. Any ideas why I'm getting this error?
EDIT: Implementation of service classes
public class GoogleService implements Parcelable {
private String name;
private String uri;
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
protected GoogleService(Parcel in) {
name = in.readString();
uri = in.readString();
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(uri);
}
#SuppressWarnings("unused")
public static final Parcelable.Creator<GoogleService> CREATOR = new Parcelable.Creator<GoogleService>() {
#Override
public GoogleService createFromParcel(Parcel in) {
return new GoogleService(in);
}
#Override
public GoogleService[] newArray(int size) {
return new GoogleService[size];
}
};
}
This looks extremly strange
this.services = in.readParcelable(getClass().getClassLoader());
You are pointing out to the wrong class loader. You have to use it like this
this.services = (AllServices)in.readParcelable(AllServices.class.getClassLoader());
Besides that, I am always using this site http://www.parcelabler.com/ to quickly generate my Parceables. Or you could use it like this
services = (AllServices) in.readValue(AllServices.class.getClassLoader());
Here:
protected AllServices(Parcel in) { this.gigrev = in.readParcelable(getClass().getClassLoader());
this.apple = in.readParcelable(getClass().getClassLoader());
this.google = in.readParcelable(getClass().getClassLoader());
this.spotify = in.readParcelable(getClass().getClassLoader()); }
Replace it with
protected AllServices(Parcel in) { this.gigrev = in.readParcelable(GigRevService.class.getClassLoader());
this.apple = in.readParcelable(AppleService.class.getClassLoader());
this.google = in.readParcelable(GoogleService.class.getClassLoader());
this.spotify = in.readParcelable(SpotifyService.class.getClassLoader()); }
And replace:
this.services = in.readParcelable(getClass().getClassLoader());
With:
this.services = in.readParcelable(AllServices.class.getClassLoader());
Also can you show the GoogleService etc classes? Each inner object's class must have a Parcelable creator also and implement the Parcelable interface as well.

Android Intent Parcelable: Class not found when unmarshalling

I looked all answers to question "Class not found when unmarshalling" in the Stackoverflow, but I don't found solution to my situation. I tried any variants but didn't work.
I get following error:
Here my Classes:
Class one:
public class Class1 implements Parcelable, Serializable {
private static final long serialVersionUID = -3892107077759983950L;
private long id;
private ArrayList<Class2> details;
public Class1(long id, ArrayList<Class2> details) {
this.id = id;
this.details = details;
}
public Class1(Parcel in) {
this.id = in.readLong();
details = new ArrayList<Class2>((Collection<? extends Class2>) Arrays.asList(in.readParcelableArray(Class2.class.getClassLoader())));
// details = (ArrayList<OrderDetail>)in.readSerializable();
// details = in.readArrayList(Class2.class.getClassLoader());
// in.readList(details, Class2.class.getClassLoader());
// in.readTypedList(details, Class2.CREATOR);
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(id);
// dest.writeSerializable(details);
// dest.writeList(details);
// dest.writeTypedList(details);
dest.writeParcelableArray(ordDetailListToArr(), flags);
}
#Override
public int describeContents() { return 0; }
public static final Parcelable.Creator<Class1> CREATOR
= new Parcelable.Creator<Class1>() {
public Class1 createFromParcel(Parcel in) {
return new Class1(in);
}
public Class1[] newArray(int size) {
return new Class1[size];
}
};
}
Class second:
public class Class2 implements Parcelable, Serializable {
private static final long serialVersionUID = 3242734374178052427L;
private long orderId;
private Class3 Class3;
public Class2(long orderId, Class3 Class3) {
this.orderId = orderId;
this.Class3 = Class3;
}
private Class2(Parcel in) {
this.orderId = in.readLong();
this.Class3 = (Class3) in.readParcelable(Class3.class.getClassLoader());
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(orderId);
dest.writeParcelable(Class3, flags);
}
#Override
public int describeContents() { return 0;}
public static final Parcelable.Creator<Class2> CREATOR
= new Parcelable.Creator<Class2>() {
public Class2 createFromParcel(Parcel in) {
return new Class2(in);
}
public Class2[] newArray(int size) {
return new Class2[size];
}
};
}
Class third:
public class Class3 extends Class4 implements Cloneable, Serializable {
private static final long serialVersionUID = 8712386538880552241L;
private int count;
public Class3(Class4 Class4, int count) {
super(Class4.getId(), Class4.getName());
this.count = count;
}
private Class3(Parcel in) {
super(in);
this.count = in.readInt();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeInt(count);
}
#Override
public int describeContents() { return 0;}
public static final Parcelable.Creator<Class3> CREATOR
= new Parcelable.Creator<Class3>() {
public Class3 createFromParcel(Parcel in) {
return new Class3(in);
}
public Class3[] newArray(int size) {
return new Class3[size];
}
};
}
class fourth:
public class Class4 implements Parcelable, Serializable {
private static final long serialVersionUID = -8394588748958519736L;
private long id;
private String name;
public Class4(long id, String name) {
this.id = id;
this.name = name;
}
public Class4(Parcel in) {
this.id = in.readLong();
this.name = in.readString();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(id);
dest.writeString(name);
}
#Override
public int describeContents() { return 0;}
public static final Parcelable.Creator<Class4> CREATOR
= new Parcelable.Creator<Class4>() {
public Class4 createFromParcel(Parcel in) {
return new Class4(in);
}
public Class4[] newArray(int size) {
return new Class4[size];
}
};
}
I used Serializable in all classes for exchange(read/write object) data via Socket channel.
I passed data from my first activity to second activity following way:
First Activity:
Class1 class1 = ...;
Intent secondActivity = new Intent(FirstActivity.this, SecondActivity.class);
orderedProductsActivity.putExtra("mydata", (Parcelable)class1);
startActivity(secondActivity);
Second Activity:
Class1 class1 = (Class1) getIntent().getParcelableExtra("mydata");
I had this issue in the past, in my case it was related with ArrayList, can you try to do:
array = (ArrayList<Long>) in.readSerializable();
and
dest.writeSerializable(array);
instead of using writeParcelableArray

Android Parceable issue

I am currently trying to pass a parceablearraylist from mylogin activity to my main activity but getting a
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.muuves.introround/com.muuves.introround.activities.MainActivity}: java.lang.RuntimeException: Parcel android.os.Parcel#41ef90a8: Unmarshalling unknown type code 7536741 at offset 684
Here are my parceable objects
public class Challenge implements Parcelable {
private Integer id;
private Integer challenger_id;
private Integer friend_id;
private String artist_name;
private String song_name;
private String image_url;
private String song_url;
private String answer_url;
private int challenge_attempts_left;
private int answer;
private Person person;
private Person friend;
public Challenge() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getChallenger_id() {
return challenger_id;
}
public void setChallenger_id(Integer challenger_id) {
this.challenger_id = challenger_id;
}
public Integer getFriend_id() {
return friend_id;
}
public void setFriend_id(Integer friend_id) {
this.friend_id = friend_id;
}
public String getArtist_name() {
return artist_name;
}
public void setArtist_name(String artist_name) {
this.artist_name = artist_name;
}
public String getSong_name() {
return song_name;
}
public void setSong_name(String song_name) {
this.song_name = song_name;
}
public String getImage_url() {
return image_url;
}
public void setImage_url(String image_url) {
this.image_url = image_url;
}
public String getSong_url() {
return song_url;
}
public void setSong_url(String song_url) {
this.song_url = song_url;
}
public String getAnswer_url() {
return answer_url;
}
public void setAnswer_url(String answer_url) {
this.answer_url = answer_url;
}
public int getChallenge_attempts_left() {
return challenge_attempts_left;
}
public void setChallenge_attempts_left(int challenge_attempts_left) {
this.challenge_attempts_left = challenge_attempts_left;
}
public int getAnswer() {
return answer;
}
public void setAnswer(int answer) {
this.answer = answer;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public Person getFriend() {
return friend;
}
public void setFriend(Person friend) {
this.friend = friend;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeInt(challenger_id);
dest.writeInt(friend_id);
dest.writeString(artist_name);
dest.writeString(image_url);
dest.writeString(song_url);
dest.writeString(answer_url);
dest.writeInt(challenge_attempts_left);
dest.writeInt(answer);
dest.writeParcelable(person, flags);
dest.writeParcelable(friend, flags);
}
public static final Parcelable.Creator<Challenge> CREATOR = new Parcelable.Creator<Challenge>() {
#Override
public Challenge createFromParcel(Parcel in) {
Challenge challenge = new Challenge();
challenge.setId(in.readInt());
challenge.setChallenger_id(in.readInt());
challenge.setFriend_id(in.readInt());
challenge.setArtist_name(in.readString());
challenge.setSong_name(in.readString());
challenge.setImage_url(in.readString());
challenge.setSong_url(in.readString());
challenge.setAnswer_url(in.readString());
challenge.setChallenge_attempts_left(in.readInt());
challenge.setAnswer(in.readInt());
challenge.setPerson((Person) in.readParcelable(Person.class.getClassLoader()));
challenge.setFriend((Person) in.readParcelable(Person.class.getClassLoader()));
return challenge;
}
#Override
public Challenge[] newArray(int size) {
return new Challenge[size];
}
};
}
public class Person implements Parcelable {
private int id;
private String first_name;
private String last_name;
private String image_url;
public Person(){
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirst_Name() {
return first_name;
}
public void setFirst_Name(String first_name) {
this.first_name = first_name;
}
public String getLast_Name() {
return last_name;
}
public void setLast_Name(String last_name) {
this.last_name = last_name;
}
public String getImage_url() {
return image_url;
}
public void setImage_url(String image_url) {
this.image_url = image_url;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(first_name);
dest.writeString(last_name);
dest.writeString(image_url);
}
public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
#Override
public Person createFromParcel(Parcel in) {
Person person = new Person();
person.setId(in.readInt());
person.setFirst_Name(in.readString());
person.setLast_Name(in.readString());
person.setImage_url(in.readString());
return person;
}
#Override
public Person[] newArray(int size) {
return null;
}
};
}
Login activity
#Override
public void preLoadDetails(Details details) {
Intent intent = new Intent(this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("receive", details.getReceive());
intent.putExtras(bundle);
startActivity(intent);
}
MainActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getIntent().getExtras();
ArrayList<Challenge> friends = bundle.getParcelableArrayList("receive");
mContent = new MainFragment(0);
mContent.setArguments(getIntent().getExtras());
// set the Above View
setContentView(R.layout.home_frame);
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, mContent).commit();
// set the Behind View
setBehindContentView(R.layout.menu_frame);
getSupportFragmentManager().beginTransaction()
.replace(R.id.menu_frame, new MenuFragment()).commit();
// Disable
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
}
Been trying to fix it for the pass day and getting no where all I know is that person parceable object in the challenge object are always null but they are not when I put them in the bundle.
Appreciate any input.
Those Integer type could be null or an int, you need to handle that situation properly.
For example I have utility functions like this :
static public Integer readInteger(Parcel in){
if(in.readInt() == 1){
return in.readInt();
}else{
return null;
}
}
static public void writeInteger(Integer value, Parcel out){
if(value != null){
out.writeInt(1);
out.writeInt(value);
}else{
out.writeInt(0);
}
}
First thing in the class Challenge you use Integer attributes and you parce "int" those are 2 diferents things.
Exemple of how i do things myself :
public Podcast(Parcel parcel) {
id = parcel.readLong();
title = parcel.readString();
subtitle = parcel.readString();
summary = parcel.readString();
duration = parcel.readString();
author = parcel.readString();
date = parcel.readString();
mp3 = parcel.readString();
podcastCategoryId = parcel.readLong();
}
#Override
public int describeContents() {
// not used
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(id);
dest.writeString(title);
dest.writeString(subtitle);
dest.writeString(summary);
dest.writeString(duration);
dest.writeString(author);
dest.writeString(date);
dest.writeString(mp3);
dest.writeLong(podcastCategoryId);
}
public static Creator<Podcast> CREATOR = new Creator<Podcast>() {
public Podcast createFromParcel(Parcel parcel) {
return new Podcast(parcel);
}
public Podcast[] newArray(int size) {
return new Podcast[size];
}
};
#Override
public Person[] newArray(int size) {
return null;
}
You should create there an array of that size and return it.
#Override
public Person[] newArray(int size) {
return new Person[size];
}
Solved it was actually very simple, forgot song url when i was writing to the parcel. Any when i missed that write that was why i was getting the weird exception for person object. it was expecting a person object but was getting something else. 1 day wasted oh well :(
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeInt(challenger_id);
dest.writeInt(friend_id);
dest.writeString(artist_name);
**dest.writeString(song_name);**
dest.writeString(image_url);
dest.writeString(song_url);
dest.writeString(answer_url);
dest.writeInt(challenge_attempts_left);
dest.writeInt(answer);
dest.writeParcelable(person, flags);
dest.writeParcelable(friend, flags);
}

Android: OutOfMemoryError when unmarshalling a Parcelable

i got a serious problem while trying to unmarshall an ArrayList of custom objects, with an ArrayList of custom objects as a field in it. When i remove everything that has to do with the ArrayList<User> in the Coon.java everything works fine. The error occurs, after including the "readTypedList"-part....Trying to fix that problem the whole day and can't find any solution.
Thanx in advance,
Roberto
Stacktrace: http://pastebin.com/LAGYmW95
Code:
-> Coon.java <-
package development.coonDub.misc;
import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;
public class Coon implements Parcelable {
/**
*
*/
public Coon(Parcel in) {
readFromParcel(in);
}
String name;
String city;
String country;
ArrayList<User> pts = new ArrayList<User>();
public Coon(String name, String city, String country,ArrayList<User> pts) {
this.name = name;
this.city = city;
this.country = country;
this.pts = pts;
}
public String getName() {
return name;
}
public String getCity() {
return city;
}
public String getCountry() {
return country;
}
public ArrayList<User> getPts() {
return pts;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(city);
dest.writeString(country);
dest.writeTypedList(pts);
}
private void readFromParcel(Parcel in) {
name = in.readString();
city = in.readString();
country = in.readString();
in.readTypedList(pts, User.CREATOR);
}
public static final Parcelable.Creator<Coon> CREATOR =
new Parcelable.Creator<Coon>() {
public Coon createFromParcel(Parcel in) {
return new Coon(in);
}
public Coon[] newArray(int size) {
return new Coon[size];
}
};
}
-> User.java <-
package development.coonDub.misc;
import android.os.Parcel;
import android.os.Parcelable;
public class User implements Parcelable {
String name;
public static final Parcelable.Creator<User>
CREATOR = new Parcelable.Creator<User>() {
public User createFromParcel(Parcel in) {
return new User(in);
}
public User[] newArray(int size) {
return new User[size];
}
};
public User(String name) {
this.name = name;
}
public User(Parcel in) {
readFromParcel(in);
}
private void readFromParcel(Parcel in) {
name = in.readString();
}
public String getName() {
return name;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
}
}
-> Activity1.class <-
...
Intent notificationIntent =
new Intent(getApplicationContext(), Activity2.class);
// coons is an ArrayList<Coon>..
notificationIntent.putExtra("coons", coons);
...
-> Activity2.class <-
...
ArrayList<Coon> coons = getIntent().getExtras().getParcelableArrayList("coons");
...

Categories

Resources