0

Excude me if this is a dumb question.

I have a "Fragment" outer-class with and inner-class interface. This interface is only implemented by one other Activity-class using "implements OuterFragment.ParentActivityListener".

I would like to have a few constants to use with the interface methods. But these constants also needs to be available in the outer-class. Is there a way to access them from the outer-class as shown below? Is this a bad pattern to use, even though there will be a very limited use of this interface (=1)?

public class OuterFragment extends Fragment {

    public interface ParentActivityListener {
        public int OKBUTTON = 5;
        public void onPlayertimerMessage(int idFromFragment, int idFromPosition, int iAction);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, 
            ViewGroup container, Bundle savedInstanceState) {
          // Access interface constant from here?
    }

}

Any input appreciated!

Koniak
  • 484
  • 1
  • 4
  • 13

1 Answers1

1

If it's a constant, it should be static and final :

public static final int OKBUTTON = 5;

And you access it with OuterFragment.ParentActivityListener.OKBUTTON.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 1
    Are you sure that `OuterFragment.` is needed? The `ParentActivityListener` interface should be in scope of the inner class. – aioobe Oct 09 '14 at 19:44
  • @aioobe I think it's better practice to use the full name. It should work without OuterFragment if accessed from within OuterFragment. – Eran Oct 09 '14 at 19:45
  • thank you works great! Is it better to be using enum instead in the interface class? (It works also without the "OuterFragment.", but I will keep it there.) – Koniak Oct 09 '14 at 19:51
  • Changed to enum in the inner-class interface, since that seems to be better practice. – Koniak Oct 09 '14 at 19:57