How to use an interface in multiple classes in Java?
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) {
}
}
Solution
I think you can use observer mode, which is a good practice in your case.
As described by GoF:
“Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically”.
Read more http://www.java2blog.com/2013/02/observer-design-pattern-in-java.html#TLio7G2ruqxvfZUR.99
In your case, you have such a relationship (one-to-many) that you want to know both fragments when an event occurs in the activity.
fragment is the implementation observer class, and your activity has the principal role, as shown in the preceding figure.
I hope this helps you implement your code in a very good way.
Some tutorials can be found at the following link:
https://dzone.com/articles/observer-pattern-java
http://www.tutorialspoint.com/design_pattern/observer_pattern.htm
EDIT: In the given case:
public interface OnReceiveListener { // this is your observer interface !
This can be any number of events to be sent to the activity
void receivePreview(Object... obj);
}
fragments are correctly defined in this design pattern, so I don’t change their code:
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) {
}
You need to reference the fragment in the activity (as an observer).
ArrayList< OnReceiveListener > observers = new ArrayList< OnReceiveListener>();
In fact, observers can subscribe or register themselves to a topic (fragments hold references to activities (preferably using singleton mode!:D). Like this:
public class MainAct extends AppCompatActivity {
private static MainAct instance;
public static MainAct getInstance() {
if(instance != null)
return instance;
}
I think it is better to create the instance variable in the onCreate() method of the MainAct activity
onCreate(...)
{
.
.
.
instance = this;
...
}
public void registerObserver(OnReceiveListener observer){
observers.add(observer)
}
/* To avoid memory leaks, remember to unregister receivers when no longer observing */
public void unregisterObserver(OnReceiveListener observer) {
observers.remove(observer);
}
notifyObservers(){
call this method in the listener you want
for( Observer obser : observers)
obser. receivePreview(param )
}
...
in fragment initialization:
MainAct.getInstance().registerObserver(this)