metis
metis

Reputation: 1044

Why an activity object is casted to an interface in Android?

In the following link https://github.com/iPaulPro/Android-ItemTouchHelper-Demo/blob/master/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/MainFragment.java , Activity object is casted to an inner interface named OnListItemClickListener.

mItemClickListener.onListItemClick(position);

The whole source code of the MainFragment class is as the following :

public class MainFragment extends ListFragment {

    public interface OnListItemClickListener {
        void onListItemClick(int position);
    }

    private OnListItemClickListener mItemClickListener;

    public MainFragment() {
    }

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

        mItemClickListener = (OnListItemClickListener) activity;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        final String[] items = getResources().getStringArray(R.array.main_items);
        final ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
                android.R.layout.simple_list_item_1, items);
        setListAdapter(adapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        mItemClickListener.onListItemClick(position);
    }
}

Why does activity object casted to an interface?

There is similar question at stackoverflow How is it possible to cast an Android Activity to an interface? but it is not satisfactory for me, it does not reply my questions.

Upvotes: 0

Views: 129

Answers (3)

Murat Karag&#246;z
Murat Karag&#246;z

Reputation: 37604

To expand on @CommonsWare answer on why you have to cast it into OnListItemClickListener. The onAttach lifecycle method of the Fragment accepts any Activity - and an Activity does not have MainFragment.OnListItemClickListener implemented as default.

So the compiler does not know that, but you know that MainActivity has implemented it and with the cast you tell the compiler that MainActivity implements MainFragment.OnListItemClickListener.

Upvotes: 2

Em&#237;lio Fonseca
Em&#237;lio Fonseca

Reputation: 92

You have to make your activity implements MainFragment.OnListItemClickListener.

Besides, in your Activity, you will ned to implement the function void onListItemClick(int position).

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006944

This interface is not implemented in anywhere in the codes or i cannot see it

See this line:

public class MainActivity extends ActionBarActivity implements MainFragment.OnListItemClickListener {

but i cannot see any other implementation of this method..

See these lines:

@Override
public void onListItemClick(int position) {
    Fragment fragment = null;
    switch (position) {
        case 0:
            fragment = new RecyclerListFragment();
            break;

        case 1:
            fragment = new RecyclerGridFragment();
            break;
    }

    getSupportFragmentManager().beginTransaction()
            .replace(R.id.content, fragment)
            .addToBackStack(null)
            .commit();
}

Why does activity object casted to an interface?

This is a typical implementation of the contract pattern. The fragment requires that its hosting activity implement a certain interface. That way, the fragment does not care what specific activity class is the one that hosts it.

Upvotes: 1

Related Questions