On Sun, Feb 27, 2011 at 8:59 PM, davemac <davemac327@gmail.com> wrote:
A little more formally, have your activity implement an interface such
as OnMyFragmentListener, so that the activity must implement a method
like onFragmentDidSomething(). When your fragment wants to send
something to the activity, it can do the following:
((OnMyFragmentListener)getActivity()).onFragmentDidSomething(...);
You will get a ClassCastException in your fragment if the activity
didn't implement the interface, but the really nice thing is that from
your fragment's point of view, it doesn't have to care exactly which
activity did the invoking. The cast is to the interface, not the
activity. You could put the Listener interface definition inside your
fragment class to keep things nice and clean.
- dave
On Feb 27, 9:58 pm, Gregg Reno <gregg.r...@gmail.com> wrote:
> OK, that makes sense. Thanks Dianne.
> -Gregg
> On Feb 27, 2011 9:53 PM, "Dianne Hackborn" <hack...@android.com> wrote:
>
> > Fragment.getActivity().doSomething().
>
> > A little more formally, define an interface for the fragment to call back
> on
> > the activity it is running in, which each activity using that fragment can
> > implement.
>
> > On Sun, Feb 27, 2011 at 6:39 PM, Gregg Reno <gregg.r...@gmail.com> wrote:> > hack...@android.com
>
> >> Maybe this covered earlier in this thread and I'm still not
> >> understanding it. I like the idea of a each fragment communicating
> >> back to the activity rather than to other fragments. Then the activity
> >> can decide whether it needs to send information to other fragments.
>
> >> So is there an existing mechanism for a fragment to send a message to
> >> the activity? If not, I guess I could just create a handler in the
> >> activity and pass the handler pointer to the fragment so it can send a
> >> message to the activity.
>
> >> My use case is the activity is responsible for all changes to the
> >> action bar and to showing or hiding various fragments.
>
> >> Thanks,
> >> -Gregg Reno
>
> >> --
> >> 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+unsubscribe@googlegroups.com
> >> For more options, visit this group at
> >>http://groups.google.com/group/android-developers?hl=en
>
> > --
> > Dianne Hackborn
> > Android framework engineer
>--
> > 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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
Dianne Hackborn
Android framework engineer
hackbod@android.com
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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
No comments:
Post a Comment