choman choman - 5 months ago 12
Android Question

How to implement an interface on a NavigationDrawerActivity?

I am currently implementing an interface in a blank activity without any problem but then when I have to shift to another activity, for example an activity that already implements something like a navigational drawer I am confused as to how to implement my interface. for example for the interface below

public interface TabsListener {
void onTabAdded();
void onTabRemoved();
}


I would implement it this way on a blank activity

public class MainActivity extends AppCompatActivity implements TabsListener {


but then again for an Navigation drawer activity which already implements a Listener do I have to include my interface as a sub class or can it be done from the main method ?

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigatioinItemSelectedListener {}

Answer

In java you can extend only one class but you can implement all the interfaces you want.

public class MainActivity extends AppCompatActivity implements Interface1, Interface2 {

    private Interface1 mListener1;
    private Interface2 mListener2;
}

If you want to create a new interface that extends functionalities of another one, could be nice extend directly the base interface:

Interface1 {
      public void method1();
}

Interface2 extends Interface1 {
      public void method2();
}

class MyClass implements Interface2 {

      @Override public void method1(){}
      @Override public void method2(){}
}
Comments