Home » Android » java – Observable/Observer not working?

java – Observable/Observer not working?

Posted by: admin June 15, 2020 Leave a comment


I tried implementing a static Observable in my Application subclass, but it’s not working. No exceptions or error messages, but my update() callback doesn’t get called.


public class MyApplication extends Application{
    public static Observable appObserver = new Observable();

    public void onCreate(){



MyApplication.appObserver.notifyObservers( "Hello world" );


public class BarFragment extends Fragment implements Observer{

    public void onActivityCreated(Bundle savedInstanceState) {

    public void onDestroy() {       

    public void update(Observable observable, Object data) {
        Log.i("BarFragment", data.toString()); // Should log, but doesn't


What’s more, I tried scribbling down a simple Observable of my own, and then everything worked like a charm with just substituting public static Observable appObserver = new Observable(); with public static MyObservable appObserver = new MyObservable();


public class MyObservable {
    protected List<Object> observers = new ArrayList<Object>();

    public void addObserver(Object observer){

    public void notifyObservers(Object data){
        for( int i=0; i<observers.size(); i++ ){
            ((Observer) observers.get(i)).update(null, data);

What am I missing?

I’m testing this on a Nexus One with Android 2.3.6 if it matters.

How to&Answers:

Seems I missed a crucial part of the Observer mechanism, apparently one has to call setChanged() before notifyObservers(). But the thing is, setChanged() is protected and thus only visible to any subclass of Observable.

So I guess it’s not meant to be used in the fashion I tried to use it, but the hackish subclass below will work…

public class MyObservable extends Observable{

    public boolean hasChanged() {
        return true; //super.hasChanged();



BadCash’s answer actually didn’t work for me, but my issue did stem from the same problem with Observable. Perhaps you’re using an older implementation, but I’m on Java 8. Rather than overriding hasChanged(), I had to override notifyObservers() to always set itself to be changed before notifying:

class WorkingObservable extends Observable{
    public void notifyObservers(){

That did the trick for me. I looked at the source code and it seems that the Observable class doesn’t call hasChanged() method internally to decide whether to notify. It just checks the changed private variable. This probably could have been implemented better.