I think Dave's code can be slightly changed to have the desired affect
and still maintain constructor semantics

public class MyFragment extends Fragment {
    public MyFragment() {}
    public MyFragment(int index) {
//        MyFragment f = new MyFragment();
        Bundle args = new Bundle();
        args.add("index", index);
        this.setArguments(args);
    }

Notice the commented out instantiation and uses the "this.setArguments()".

Satya


On Mon, Feb 14, 2011 at 9:47 AM, Kostya Vasilyev <kmans...@gmail.com> 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 android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en



-- 
Satya Komatineni
http://www.satyakomatineni.com
http://www.androidbook.com

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

Reply via email to