Question

I noticed that sharing data between activities can be performed with 2 methods !

1st Method :

 Intent myIntent = new Intent (view.getApplication,myActivity.class);
 myIntent.putExtra("data","value");

2nd Method :

Intent myIntent = new Intent (view.getApplication,myActivity.class);
Bundle myBundle = new Bundle();
myBundle.putString("data","data");
myIntent.putExtra(myBundle);

My question here is what's actually the difference between those 2 methods ?

Était-ce utile?

La solution

As you can see from Intent.java source code, data passed to Intent always serialized into Bundle object. So difference is "how your data will be serialized/deserialized"

public Intent putExtra(String name, String value) {
    if (mExtras == null) {
        mExtras = new Bundle();
    }
    mExtras.putString(name, value);
    return this;
}

public Intent putExtra(String name, Bundle value) {
    if (mExtras == null) {
        mExtras = new Bundle();
    }
    mExtras.putBundle(name, value);
    return this;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top