fweigl
fweigl

Reputation: 22028

Dummy callback interface

In the example master-detail-flow code from Google that Eclipse creates for you, theres this stuff in the fragment:

private Callbacks mCallbacks = sDummyCallbacks;

public interface Callbacks {
    public void onItemSelected(String id);
}

private static Callbacks sDummyCallbacks = new Callbacks() {
    @Override
    public void onItemSelected(String id) {
    }
};


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    mCallbacks = (Callbacks) activity;
}

@Override
public void onDetach() {
    super.onDetach();
        // Reset the active callbacks interface to the dummy implementation.
    mCallbacks = sDummyCallbacks;
}

I understand how a callback interface is used to communicate from a fragment to it's containing Activity, but what is this dummy callback good for?

Upvotes: 5

Views: 1725

Answers (1)

njzk2
njzk2

Reputation: 39406

The dummy callback is made to avoid the need for testing the validity of the callback when using it.

The other way to 'reset' the callbacks in onDetach is to set it to null, but that would require null testing every time it is used, which is a lot of repetitive/boring code.

Upvotes: 8

Related Questions