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 ?

Was it helpful?

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top