Yes I really suggest following the model in the sample, with a static function to create the fragment with its arguments. It is not any more lines of code, and avoids making mistakes like forgetting to declare an empty constructor.
On Mon, Feb 14, 2011 at 6:47 AM, Kostya Vasilyev <[email protected]> wrote: > The code fragment below creates two instances of MyFragment, which is > probably not what you intended. > > One is created by the new operator outside the constructor (someone doing > "new MyFragment(myIndexValue)") and another instance inside the constructor > ("f = new MyFragment"). The latter one, with the index value, is not > returned to the caller in any way, it just gets dropped on the floor. > > What you probably want is a factory method, like this: > > public class MyFragment extends Fragment { > public *static MyFragment *createMyFragment(int index) { > MyFragment f = new MyFragment(); > Bundle args = new Bundle(); > args.*putInt*("index", index); > f.setArguments(args);* return f;* > } > } > > which is remarkably similar to "public static DetailsFragment > newInstance(..)" in Dianne's blog post, see 3rd code example from the top. > > Now clients of MyFragment can do "MyFragment f = > MyFragment.createMyFragment(myIndexValue);" > > -- Kostya > > 14.02.2011 17:20, davemac пишет: > > public class MyFragment extends Fragment { > public MyFragment() {} > public MyFragment(int index) { > MyFragment f = new MyFragment(); > Bundle args = new Bundle(); > args.add("index", index); > f.setArguments(args); > } > > [ ... ] > } > > Now the clients of MyFragment can instantiate one with arguments much > easier, yet I still get the recoverability because I do the right > things behind the scenes. > > > > -- > Kostya Vasilyev -- http://kmansoft.wordpress.com > > -- > 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

