1

If I pass a bundle to an activity from MyActivity to MyFragment using setArgs and getArgs. Couldn't I have just passed the data objects through the MyFragment constructor? What's the point of all this bundle stuff? Is it faster?

Side question: When I pass bundles around, are they aliased? So that editing the bundle will change everything?

3

3 Answers 3

0

The bundle is needed to manage the lifecycle of the fragment and to allow the OS to create again the object. Other info here docs

0

Well, I think this needs some introducing:

If an Activity or Fragment will be f.e. destroyed the state of them will be saved. Therefor this state must be saved by the system to be able to recreate it. The best thing to do is to save it as a byte code with all information necessary to rebuild the objects which define this state. With the use of byte code it doesn't matter of which type this objects are. The transformation of objects into byte code is called serialization.

Android has it's own serialization mechanism called Parcelable. It's much faster than the serialization Java provides by default and because of this it should be the prefered way to use in Android.

Now Bundles only take Objects, which implements the Parcelable or the Serializable interface, and primitives. They are used to save the different objects which define the state of Activities/Fragments at one place.

Because the arguments which you can pass to a Fragment will also be serialized by the system for future use, a Bundle is needed. This is the reason why a Fragment should only use a non-arg constructor and pass the parameters through the arguments.

In short: A Bundle is used by the system to save and recreate the state of Activities or Fragments. Therefor the system uses a format for this data which is easily readable, a byte code.

-3

the big deal in budnles is that they work between applications. This makes for the possibility to run, for example, the browser, with a website that is programmed in the app. If you would start the browser whilst passing data through a constructor, the browser window would run in the same thread as the application that you started running it through. Bundles and intents make the operating system more secure.

1
  • and I don't think they are aliased, and that once the intent is sent, the information added to it, will stay the same for the receiving end. Jun 8, 2014 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.