Android: Having Difficulty Passing An Object From One Activity To Another Using Parcel
Solution 1:
You've got a bigger problem here. You're not supposed use Parcels to pass things between activities. Per the android documentation:
Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.
You should use bundles instead. What I usually do is create two static methods in the class I'm trying to pass between activities called toBundle and fromBundle. The toBundle takes an instance of the class I want to transport and returns a bundle. The fromBunle takes a bundle and returns a instance of the class.
I'm not aware of any way to pass a complex object like a PrintWriter between Activities. But there's other problems with your code too. You're doing network stuff on the main UI thread. This is gonna cause your app to freeze A LOT. You need to move all of your IO off to a different thread. This is good because then you can just move all your IO stuff into a seperate class that's accessible from both activities. For instance, use an IntentService. This will make your app sooooooo much better. You might also want to read the article Painless Threading. It's a crucial read that I recommend for all android developers.
Solution 2:
You can't pass a PrintWriter between two Android activities using any of the normal Android data-passing methods (Bundles / Parcels). If you must share the PrintWriter between two Activities, than you can use a static variable.
That being said, you could also write a service that you access and use on each Activity. You might want to look at the LocalService example in the ApiDemos project for an example on how to do that. There are also countless tutorials online regarding this.
Post a Comment for "Android: Having Difficulty Passing An Object From One Activity To Another Using Parcel"