Lilian Sorlanski
Lilian Sorlanski

Reputation: 435

How to reffer to an interface from within an anonymous class in Android?

I have the following class:

public class MyFragment extends Fragment implements Observer<List<User>>

And

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String s) {return false;}

    @Override
    public boolean onQueryTextChange(String searchText) {
        if (searchText.length() > 0) {
            myLiveData.removeObservers(getViewLifecycleOwner(), this);
        }

        return false;
    }
});

To the removeObservers() function I need to pass a LifecycleOwner object as the first argument (which works) and as the second argument on Oberver object. When using the above line of code, it fails because of the second this is not referring the interface that is implemented in the fragment. How to make this point to the right interface and get the corresponding Oberver object?

Upvotes: 0

Views: 51

Answers (1)

Saran Sankaran
Saran Sankaran

Reputation: 2606

MyFragment.this is what you are looking for

Upvotes: 1

Related Questions