I follow this link Pass data from activity to service for understanding the passage of data from Activity to Service Android. The problem is that I need to pass a complex object to Service. How can I achieve this task?
You can make your complex object implement Serializable or Parcelable and then you can pass it to Service using putExtra(String name, Serializable value) or putExtra(String name, Parcelable value)
Here you can find a tutorial for implementing Parcelable and here Serializable one.
If you aren't familiar with an any of them I would suggest you to use `Serializable' since it's more simple and pretty straightforward
Passing in custom objects is a little more complicated. You could just mark the class as Serializable
and let Java take care of this. However, on the android, there is a serious performance hit that comes with using Serializable. The solution is to use Parcelable.
Refer the examples:
http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/
http://prasanta-paul.blogspot.com/2010/06/android-parcelable-example.html
Related
I want to share object from one activity to another activity. I know of 2 good ways:
Using bundle: By making object's class implement Parcelable, can pass object in bundle via intent.
Using singleton pattern: saving the object instance in this class and then fetch it where ever required.
Which of the above mentioned is better or recommended way? Kindly also tell if there is some other better way.
Singleton isn't a good ideia on Activities, because it will produce memory leak (redundancy of information of an Activity).
Parcelable it's a good idea, because you will not use disk IO or Network when you create a local object from a form in the first Activity.
If you retrieve an object from Network or local database, you don't need anyone, because you can retrieve the object in the next Activity.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to send an object from one Android Activity to another using Intents?
I want to pass a class object from an Activity to other when one Activity calls to the other. I am trying to use Intent.putExtra(name, value) to make that but then I donĀ“t have Intent.getExtra(name) method. There are a lot of methods like getStringExtra(name), getDataExtra(name) and so on but what I put in the value of putExtra is a class object that have a lot of different values as String, Data and int. How can I make to pass that class?
You can use Serializable or Parcelable
If you use Serializable you only have to implement it by writing implements Serializable
If you use Parcelable you have to fill in the used methodes. See: http://developer.android.com/reference/android/os/Parcelable.html
Read about the differences here: http://www.mooproductions.org/node/6
Conclusion: Parcelable is really alot faster but takes more time to implement.
In order to pass your custom objects through Intent, you need to make sure that your class implements Parcelable.
For more info, read this tutorial.
How do you pass an instance of ArrayList<ArrayList<HashMap<String, String>>> from one Android activity to another via an instance of Bundle?
(I could use JSON strings. I'd like to know if there are better ways.)
Thanking you kindly in advance.
You can pass it as an extra in the Intent that you use to fire up the new activity. Since ArrayList implements Serializable, you don't have to do anything special to feed it to Intent.putExtra().
In general it is not good to pass too many or too large data between activities via Intents. It's better to store them somewhere centrally and pass a lightweight identifier or something like this, so the other activity can retrieve them from the store.
E.g. you can use an Application class to store these data. An application class is always available as long as you application is running. You get it from each Activity by calling the getApplication() method.
HI,
i'm programming an android application using the StartActivityForResult() - Method.
But, i want to get as result an own class, not a string or something like that.
how can i tell the intent to use the class i wrote to give back?
You have to have your class implement Parcelable or Serializable. Also, this technique is only recommended for activities within the same application.
I have read some question here but I didn't find a solution. I have read about Parcelable, Intents, and sharing specific data within Activities from the android dev docs (both dev guide and reference).
Here's the scenario:
I have one ListActivity that fills in an object parsing an xml file, it shows a list of values, and when clicked I want to return the object that represents the item clicked to the activity that has called it, for then, call another activity with this object.
I read on how to implement Parcelable but seems not being the way. Implementing Parcelable receives a Parcel for the constructor and then reads the values from it (or at least that was what I understood). This makes no sense for me and I can't see how to implement basing on that issue. I build the object parsing the xml file, not having a Parcel.
I appreciate some clarifications on this, regards.
I believe you have three options here:
Pass some arbitrary 'id' of the object to the new activity in the intent extras, which obtains the object in the same way as the first activity. This I would recommend as it is in harmony with the way Android is designed to work.
Serialize the object using Java Serializable, then put it into the intent as an extra.
Have the object be a JSONObject and send it as a string in the intent extras.
That depends on whether the activities belong to the same process or different ones...if it is the same process then the answer is Within an application what is the best way to pass custom objects between activities? but if they belong to different processes then you are better of implementing parcelable.