简体   繁体   中英

Android : Bundle Vs Intent

I'm able to pass String (which is an object) to another activity through intent. But whenever I tried to pass some other class object (eg: "LatLng" class object) through intent to another activity, seeing runtime errors. What is the difference between sting object and LatLng class object in this context?

Note: I'm able to pass LatLng class object through bundle without any issues, but I just wanted to know the reason.

You can pass all the primitive type values in Intent as it is, like String, int, boolean etc. In case of String type by default it implements Serializable .

public final class
String
extends Object
implements Serializable CharSequence Comparable<T>

Reference

But if you have a complex object that you want to pass inside a Bundle either you have to make it Serializable or Parcelable . Since Serializable is java native implementation and it was not designed for Portable devices like Mobile. So if you really consider about performance you have to use Parcelable instead. Google itself promote the use of Parcelable .

But in your case LatLng implements Parcelable. So you can use

putParcelable(String key, Parcelable value)

And to access this use below method.

public T getParcelable (String key);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM