Reputation: 73
I am a newbie on Android development and how data pass from one activity to another is still unclear for me.
Is it a good practice to pass your own objects between activities?
Which size could they have?
EDIT: Maybe I did not explained it clearly. I know how to pass data between activities. The question is: Is it a good practice or should I better obtain data from SQLlite database? In that case, which is the maximun size I can pass to have a good performance?
Upvotes: 1
Views: 911
Reputation:
Sending object is same as sending pre defined objects like String or variables,it can be done by binding your object to Intent by using putParceble() or putSerializable() methods directly on intent or by binding object to a bundle object. But u have to make sure that your class implement either Parcelable or Serializable.
like here:
UserDefined myObject=new UserDefined();
Intent i = new Intent(this, Activity2.class);
Bundle b = new Bundle();
b.putParcelable("myObject", myObject);
i.putExtras(b);
startActivity(i);
And in receiving activity:
Bundle b = this.getIntent().getExtras();
myObject = b.getParcelable("myObject");
you can also send object without using Bundle:
Intent i=new Intent(PicActivity.this,PostPhotoActivity.class);
i.putExtra("myObject", myObject);
startActivity(i);
on receivingActivity:
UserDefined myObj=(UserDefined)getIntent().getParcelableExtra("myObject");
In Android parcelable is prefered instead of serializable.
Upvotes: 2