I implements an observer for a model's datas; I have 2 activity, that share that datas. In my first activity I set the model like:
public void refreshValue (String id, Data data){
ConnectionModel.getInstance().updateConnection(data);
In the model the updateConnection is like:
public class ConnectionModel extends Observable{
//...
synchronized Connection getConnection() {
return connection;
}
void updateConnection(Data data){
synchronized (this) {
connection.setData(data);
}
setChanged();
notifyObservers();
}
}
In the second activity I set the observer like:
public class secondView extends AppCompatActivity implements Observer {
public void observe(Observable o) {
o.addObserver(this);
}
//...
public void refreshView(){
Connection connection = ConnectionModel.getInstance().getConnection();
heartRate.setText(connection.toString());
}
@Override
public void update(Observable o, Object arg) {
refreshView();
Log.d("update", "data is change");
}
I also tried to use LiveData with a ViewModel, but same result.
Where am I doing wrong?
Thanks a lot.
You need to add the secondView Activity as the Observer
of the ConnectionModel
by calling:
ConnectionModel.getInstance().addObserver(this);
inside the secondView activity.