Mikhail
Mikhail

Reputation: 2935

using Interfaces and method onAttach(Fragments)

I know that its nob question but i cant understand what does the variable someEventListener assign?.. How can we compare activity with Interface onSomeEventListener) ?

 public interface onSomeEventListener {
    public void someEvent(String s);
  }

  onSomeEventListener someEventListener;

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
        try {
          someEventListener = (onSomeEventListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement onSomeEventListener");
        }
  }

Upvotes: 1

Views: 544

Answers (1)

Lawrence Choy
Lawrence Choy

Reputation: 6108

If an object implements a listener, it can be cast to another object with the listener type. In this case, we are casting the activity to the listener.

public class MyObject implements MyListener {

}

MyObject obj = new MyOBject();
MyListener listener = (MyListener)obj; // This is valid

Upvotes: 2

Related Questions