I have one activity and 2 fragment.
I want when in activity fire
listener.receivePreview(obj)
then
- execute : OneFragment -> receivePreview .
- execute : TwoFragment -> receivePreview .
public class MainAct extends AppCompatActivity {
public interface OnReceiveListener {
// This can be any number of events to be sent to the activity
void receivePreview(Object... obj);
}
private OnReceiveListener listener;
}
public class OneFragment extends Fragment implements OnReceiveListener{
@Override
public void receivePreview(Object... obj) {
}
}
public class TwoFragment extends Fragment implements OnReceiveListener{
@Override
public void receivePreview(Object... obj) {
}
}
I think you can use Observer Pattern that is a good practice in you situation.
Read more at http://www.java2blog.com/2013/02/observer-design-pattern-in-java.html#TLio7G2ruqxvfZUR.99
In your situation you have such relation (one-to-many) and when an event occurred in the activity you want to aware that two fragment.
Fragments are implement observer class and your activity has the role of subject as illustrate in above figure.
I hope this could help you to implements your code in a very nice way. some tutorial can be find in the following links :
https://dzone.com/articles/observer-pattern-java http://www.tutorialspoint.com/design_pattern/observer_pattern.htm
Edit: in the given situation:
Fragment are in correct definition with this design pattern so I do not change their code :
you need to have references to the fragments in the activity (as observer).
indeed an observer can subscribe or register itself to a subject ( fragment hold a reference to the activity (better to use singleton pattern ! :D). like this :
Try to create one function in each fragment which returns the interface instance
and in your activity when you call the method write following code
I use answer Sirvan Paraste.It seems that this useful solution.