Mainly because the whole process of sending an intent is not that simple. An intent can travel through the system, between processes etc... in short the object you created is not the same object that is received at the end (this can be proven if trying to extend an intent class, send it to another activity and try to cast it back to your extended class on the other end, its simply not the same object).
Now i also really hate this, thats why i have created some base classes that help me work with intents (i call them BundleWrappers) which would work something like this:
you create a POJO with getters/setters,
you fill that object and use it however you like,
then when the time comes just serialize into a bunle and deserialize it into the same object on the other end.
then you will have the same object with getters and setters in the other activity as well.
The main reason intents suck is that you have to find a way to keep track of all the keys for the extras, and the additional implementation for serializing the bundle.
still even with my method its not easy to use intents but it is the best i have found so far in terms of performance and object organization.
public abstract class BundleWrapper implements Parcelable {
protected static final String KEY_PARCELABLE = "key_parcelable";
public static final String TAG = BundleWrapper.class.getSimpleName();
public BundleWrapper() {
super();
}
abstract Parcelable getParcelable();
public Bundle toBundle(){
final Bundle bundle = new Bundle();
Parcelable parcelable = getParcelable();
if (parcelable != null) {
bundle.setClassLoader(parcelable.getClass().getClassLoader());
bundle.putParcelable(KEY_PARCELABLE, parcelable);
}
return bundle;
}
public static Object fromBundle(final Intent intent) {
return fromBundle(intent.getExtras());
}
public static Object fromBundle(final Bundle bundle) {
if (bundle != null && bundle.containsKey(KEY_PARCELABLE)) {
bundle.setClassLoader(BundleWrapper.class.getClassLoader());
return bundle.getParcelable(KEY_PARCELABLE);
}
return null;
}
}
Here is my base class,
for using it you simply extend it and implement parcelable(the retarded part of the process :):
public class WebViewFragmentBundle extends BundleWrapper implements Parcelable {
public static final String TAG = WebViewFragmentBundle.class.getSimpleName();
private String url;
public WebViewFragmentBundle() {
super();
}
public WebViewFragmentBundle(Parcel source) {
this.url = source.readString();
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
Parcelable getParcelable() {
return this;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(url);
}
public static final Parcelable.Creator<WebViewFragmentBundle> CREATOR = new Parcelable.Creator<WebViewFragmentBundle>() {
@Override
public WebViewFragmentBundle createFromParcel(Parcel source) {
return new WebViewFragmentBundle(source);
}
@Override
public WebViewFragmentBundle[] newArray(int size) {
return new WebViewFragmentBundle[size];
}
};
}
and for a use case:
public static void launchAugmentedRealityActivityForResult(final Activity context, WebViewFragmentBundle wrapper) {
final Intent intent = new Intent(context, Augmented.class);
intent.putExtras(wrapper.toBundle());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivityForResult(intent, AUGMENTED_RESULT_CODE);
}
and cast it on the other end like:
(WebViewFragmentBundle)BundleWrapper.fromBundle(getIntent());