-1

As the title suggests, what is better technique to follow in Android? I referred few docs, but couldn't find much details that I need.

Consider I have 5+ views and I want to handle their clicks at one place, then creating a ClickListener object and passing it is better or implementing the ClickListener interface in the Activity/Fragment and then passing the instance of Activity/Fragment is better and why?

EDIT

Let me add an example since most of the people are unable to understand the statement

Case-1:

private final View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        //some code
    }
};

public void doSomeTask(){
    view1.setOnClickListener(onClickListener);
    view2.setOnClickListener(onClickListener);
    view3.setOnClickListener(onClickListener);
}

Case-2:

public class SomeActivity extends AppCompatActivity implements View.OnClickListener {

    //some code
    public void doSomeTask(){
        view1.setOnClickListener(this);
        view2.setOnClickListener(this);
        view3.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        //some code
    }
}

Which is the best approach Case-1 or Case-2 ?

Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57
  • i am not sure but your question like multiple inheritance or multi level inheritance like in java? – Hasmukh Kachhatiya Apr 27 '17 at 10:09
  • 2
    Depends on wether you are going to need other click listeners in the future, I usually create a ClickListener object and use that instead of implementing the interface, I find this way a little cleaner as opposed to implementing the interface and then creating ClickListeners when needed. I don't think it changes that much anyway so use whatever you like most. – Ayoub Apr 27 '17 at 10:17
  • @Ayoub yes, in general it doesn't make much difference, but issue arises when the size of your project increases and dependency of other codes with current code keep increasing. Other things to consider is how long the activity is tied to fragments, where else it is being used, etc. In general I implement the interface, but recently came across a conflicting code. So yes, I need something more fool proof reason other than code maintenance. – Mohammed Atif Apr 27 '17 at 10:28
  • No @Hasmukhkachhatiya its more related to implementing the interface or using the object of the interface. – Mohammed Atif Apr 27 '17 at 10:31

2 Answers2

1
  • One listener can be assigned not only to two buttons but to any number of them. And not only to buttons. Other View-elements also have different events, which require listeners.
  • The less objects you create the better, as memory is allocated for each object and it is a quite limited resource, especially for mobile devices. That’s why creating one listener for several Views is better from the optimization point of view.
  • Amount of code is reduced and it becomes more readable.

For more info Check here

a.r.
  • 565
  • 1
  • 6
  • 18
  • Yea, so my question is, one listener object or implementing the listener interface? – Mohammed Atif Apr 27 '17 at 10:32
  • Check this question http://stackoverflow.com/questions/29479647/setonclicklistener-vs-onclicklistener-vs-view-onclicklistener – a.r. Apr 27 '17 at 10:35
  • You can also check here http://stackoverflow.com/questions/30082892/best-way-to-implement-view-onclicklistener-in-android – a.r. Apr 27 '17 at 10:37
  • Thanks @a.r , really appreciate your effort, but I have already seen many links including the ones your have posted. Anyways I have added examples in my questions, these might help you to update your answer. – Mohammed Atif Apr 27 '17 at 10:43
0

I think it depends.

Personally I prefer creating a separate class that implements the interface (Case-1) when possible.

If you need to use fields and methods of a certain class it is best to just implement the interface (Case-2).

There is also another case - when you don't set the interface explicitly, e.g. Fragment callback interfaces. Fragment have their own lifecycle - so using a callback setter wouldn't work. All we know is that at one point they get attached (in onAttach()) so we try casting the received Context to the interface that we need. That way you must implement the interface in the Activity that instantiates the Fragment.

Beware of some other issues. E.g. you have a singleton class that requires a listener object. You implement the interface in an Activity and you pass an instance to that class. But Activity can be destroyed and recreated - that would cause the activity instance count violation reported by StrictMode as that singleton class would still hold reference to the previous Activity instance.

LukeJanyga
  • 1,115
  • 9
  • 16