1

I am learning Android from a few tutorials, and when it comes to using OnClickListeners, I notice that a lot of them use anonymous inner classes like so:

public class MainActivity extends AppCompatActivity {

    private EditText mNameField;
    private Button mStartButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNameField = (EditText) findViewById(R.id.nameEditText);
        mStartButton = (Button) findViewById(R.id.startButton);

        mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = mNameField.getText().toString();
                Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();
            }
        });
    }

}

Is this considered good practice to have a class inside a class? Or is it considered better to somehow define this class outside somewhere and then reference it? How would I do this?

DoubleBass
  • 1,143
  • 4
  • 12
  • 29
  • Possible duplicate of [How are Anonymous (inner) classes used in Java?](http://stackoverflow.com/questions/355167/how-are-anonymous-inner-classes-used-in-java) – Andrew Regan Mar 05 '16 at 16:59
  • @AndrewRegan I know how they are used -- but I am asking if they are considered good practice. – DoubleBass Mar 05 '16 at 17:00
  • The android documentation is not too suggestive about what approach is the 'best' but if you have a few buttons in a layout you wouldn't create anonymous classes for each button. That becomes cumbersome. Instead consider implementing OnClickListener as a part of your Activity/Fragment. – Susheel Mar 05 '16 at 17:02
  • @Susheel How would I do that? I posted more code above. If I wanted to implement the listener not as an inner class, that is – DoubleBass Mar 05 '16 at 17:21

2 Answers2

1

If you have to duplicate some code, you should do some refactoring and define it only in one place. In case of using it in a single class.

public class MainActivity extends Activity {
View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //some code
    }
};

mStartButton.setOnClickListener(onClickListener);

When you use the same listener in multiple classes

public class WidelyUsedOnClickListener implements View.OnClickListener {
    @Override
    public void onClick(View v) {
    //some code
    }

} Then in your Activity

WidelyUsedOnClickLister onClickListener = new WidelyUsedOnClickLister();
mStartButton.setOnClickListener(onClickListener);
Luqasso
  • 56
  • 4
0

Inner Class Concept only came when without existing one Objcet (Class) there is no scope to exist another Object(Class). then you can create a class inside a Class.

This Concept is came in Java v1.1 when AWT is introduced in Java to perform eventhandling . As you used in your code

mStartButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    }
});

here Click event if and only there is Clickable Object exists (eg:- Button,or anyOther). So, EventListenermust be associate only with those components which actually needs it. this makes sense...

==========Alternatives=============

You may also implement OnClickListener() to any other Custom Event Handler Class. but it will be a very hectic task to do so, because if you used 6(suppose) Clickable Componenets then you must write six different Class EventHandler and pass this Class at

                    new View.OnClickListener(new CustomEventHandler()) 

Hope you got the concept.. There are so many more consequences but You should think atleast once. You Might get your answer.

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52