Actually the big reason for using a Bundle is because this needs to be saved
as part of the saved instance state of the activity.  The Bundle class
provides a data structure that is Parcelable (can be saved) and
automatically takes care of the marshalling/unmarshalling for you (so the
app developer won't need to deal with that complexity).

If you use a constructor that takes an int, you will then need to write the
code in onSaveInstanceState() to store the int and retrieve it in
onCreate().  Since you are going to be dealing with bundles, anyway, you
might as well just have it in a bundle up-front and let the framework take
care of the saving and restoring.

In fact the whole arguments API was added to fragment just to reduce the
amount I was seeing that you had to write that save/restore boilerplate
code.

Of course if you don't really care about retaining your arguments across
instances, then there is no reason to use the formal arguments bundle and go
ahead and have a constructor with the typed arguments.  That said, if you
think this is the case, there is probably a 90% chance you actually aren't
realizing that you *do* need to have those arguments saved so will run into
problems later.

Also you need to be aware that a fragment needs to have an empty constructor
so it can be re-created later from saved state, so if you do have a
constructor with arguments, you need to be sure to also explicitly write an
empty constructor.

On Sun, Feb 13, 2011 at 2:32 PM, davemac <[email protected]> wrote:

> I think I understand the logic behind using Bundles to pass in and
> store values associated with a Fragment, but I wondered if someone
> could validate my understanding.
>
> I believe that it makes sense to use a Bundle to pass in initial
> values at the time of construction of a Fragment for a number of
> reasons:
>    1) Bundles are a simple way to pass in an arbitrary number of
> arguments of different types, so the pattern of using Fragments is the
> same for everyone. This makes understanding them easier.
>    2) The arguments Bundle on a Fragment (setArguments, getArguments)
> is a convenience over having to create class member fields to keep
> track of the specifics of a Fragment object.
>    3) It is simple to take an extras Bundle from an inbound Intent
> and pass it to a Fragment's creation logic.
>
> This doesn't mean you always need to use a Bundle when creating a
> Fragment, but if you have a few or more values to pass in, a Bundle
> makes a lot of sense. Are there other reasons why you'd want to use
> Bundles in this way? Or situations where you wouldn't? The Shakespeare
> example that Dianne blogged about recently used a Bundle for a single
> int value. Was this more for demonstration purposes? Or would it have
> been acceptable to simply use a constructor that takes an int, and
> save it in a member field for later? Thanks!
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
Dianne Hackborn
Android framework engineer
[email protected]

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to