Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is it possible to cast an Android Activity to an interface?

Tags:

java

android

In the Android documentation here: http://developer.android.com/guide/components/fragments.html A Fragment implements an interface.

In the onAttach() callback, it seems to cast the current Activity to an interface. Conceptually, how is this possible and is the same type of cast standard practice in vanilla Java?

public static class FragmentA extends ListFragment {


// Container Activity must implement this interface
    public interface OnArticleSelectedListener {
        public void onArticleSelected(Uri articleUri);


    OnArticleSelectedListener mListener;



    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnArticleSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
        }
    }
    ...
}
like image 618
ZenBalance Avatar asked Aug 30 '12 19:08

ZenBalance


People also ask

What is casting how it is used in interface?

A type cast—or simply a cast— is an explicit indication to convert a value from one data type to another compatible data type. A Java interface contains publicly defined constants and the headers of public methods that a class can define.

What is interface used in Android?

The Android Interface Definition Language (AIDL) is similar to other IDLs you might have worked with. It allows you to define the programming interface that both the client and service agree upon in order to communicate with each other using interprocess communication (IPC).


2 Answers

Some basic knowledge about the topic:

Think about an interface as a bundle of functions. If a class implements an interface, then it guarantees, that it has all the interfaces functions implemented.

In your case:

If you have an object and you don't need more than the functions of an interface, that your object implements, then you can treat (and cast) that object to that interface. This way you loose "information" about your object, because you won't be able to use its functions (except the interface functions), but sometimes its enough.

like image 122
Balázs Édes Avatar answered Sep 24 '22 14:09

Balázs Édes


Any object that is created from a class which implements some interface is also an instance of that interface.

Consider this:

public class Main {
    public static void main(String[] args) {
        MyClass mine = new MyClass();
        checkIsFoo(mine);
    }

    public static void checkIsFoo(MyClass mine) {
        System.out.print(mine+"");
        if (mine instanceof MyFoo)
            System.out.println(" is Foo!");
        else
            System.out.println(" is not Foo!");
    }
    public static interface MyFoo {
        //
    }
    public static class MyClass implements MyFoo {
        //
    }
}

In this case, the following is printed out: Main$MyClass@52c8c6d9 is Foo!. This indicates that the MyClass object is also an instance of MyFoo. Since they are shared instances, calling a cast to (MyFoo) mine would also be allowed.

This is, as @antlersoft said, part of vanilla Java (and can be seen here).

Because, in this case, the Activity passed to onAttach should always be the object which is both the Activity and the OnArticleSelectedListener, casting it to OnArticleSelectedListener should always succeed.

like image 35
Cat Avatar answered Sep 21 '22 14:09

Cat