On Mon, Apr 18, 2011 at 3:07 AM, Droid <[email protected]> wrote: > If I click a button in one fragment, what is the best way to get a > response in another fragment? > > eg click button in fragment1 which displays text in a TextView in > fragment2. > > which is the best approach here?
I think it's a tad early to say what's "best". However, since the other fragment may or may not even be in memory at the moment, the pattern that I am recommending is: -- Implement an event interface -- Have the activity hosting fragment1 supply an instance of that event interface (e.g., implement the interface on the activity itself and supply 'this') -- Have fragment1 call a method on the event interface when the event occurs (e.g., button click) -- Have the hosting activity figure out what to do next (e.g., call a method on fragment2 which is already hosted in the activity, run a FragmentTransaction to add fragment2 to the activity, call startActivity() to fire up another activity for fragment2 with your data attached as extras) You'll see an example of that pattern here: https://github.com/commonsguy/cw-android/tree/master/Fragments/EU4You_6 -- Mark Murphy (a Commons Guy) http://commonsware.com | http://github.com/commonsguy http://commonsware.com/blog | http://twitter.com/commonsguy Android Training in London: http://bit.ly/smand1, http://bit.ly/smand2 -- 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

