Search code examples
javaandroidandroid-fragmentsandroid-activityandroid-event

multiple fragments and activity all responding to button click in activity


I have a multi-fragment form dividing sections of a form. When the user is finished they hit the submit button at the bottom of the screen which is owned by the hosting activity. When the user clicks submit all the fragments should send their data to the activity and then the activity handles the networking. So I have onClick listeners set for the same button in every fragment, as well as the activity. However when I hit submit onClick only runs once, in some fragment (never in the activity), when it should run 5 times (because I have 4 fragments and 1 activity). Basically I need the fragments to deal with the click and then the activity deal with the same click.


Solution

  • Use the Observer design pattern where the activity is the Subject and the fragments are the Observers