14

Where to set all Listeners for the user interfaces?
Is it good practice to set them in onCreate? This looks so unstructured and strange.
Is there a better place to set them?

ghchoi
  • 4,812
  • 4
  • 30
  • 53
user2737037
  • 1,119
  • 1
  • 16
  • 29
  • 1
    If you want the end user to click a button and do an action, you better initialize them `onCreate` and you can implement the rest in the `onClick(View v)` – Atieh Jan 18 '14 at 12:39
  • you can use constructive alternative which i have posted among in answers. – Manmohan Badaya Jan 18 '14 at 13:24

6 Answers6

15

From here: http://developer.android.com/reference/android/app/Activity.html

onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.

When you initialize your views, they are ready to be listened. onCreate is good callback to set listeners. In other way you can set it in onStart or onResume, but you should understand, that its bad practice, because onStart and onResume calls every time, when user see your activity. onCreate calls only when Activity is initialized. It is reason, why you should use onCreate. Actually, good practice implement method like initListeners() where you can put all you listeners logic.

Good luck!

ghchoi
  • 4,812
  • 4
  • 30
  • 53
Ilya Demidov
  • 3,275
  • 2
  • 26
  • 38
  • 2
    Bad answer. You should always register listeners in onStart/onResume and unregister them in onStop/onPause respectively. That's because onResume/onPause are guaranteed to be called in an activity's life cycle always but rest are not. Most people do it on onStart/onStop as it reduces multiple registers and unregisters and are mostly called, unless something goes crashing or OS kills the app – Sourabh Nov 24 '15 at 18:41
  • The question is "Where to set all Listeners for the user interfaces?". Why you need unregister them? You cannot play with your UI when Activity is stopped. People never unregister UI listsners like onClickListener and other like taht. – Ilya Demidov Nov 24 '15 at 20:43
2

Use onCreate method to set the UI and to get the Widget from UI.

protected void onCreate(Bundle savedValues) {
    // Here set the UI and get the widgets
    //set the Listeners on the widgets you are getting at the above line
}

And you can define a clickListener for the widgets and use it in onCreate method

OnClickListener someListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(v.getContext(), "widget pressed ", Toast.LENGTH_SHORT).show();

    }
};

and you can set the above clickListener to a widget which you have created in onCreate method

1

For listeners onCreate() is good place.

Consider 2 activities A,B.

A -> B, launching 'B' Activity from 'A', if we come back from B -> A then onStart(), onResume() methods will be called again in 'A' activity and that is redundant. So it's better practice to only add listeners in onCreate() only.

And, for button listeners you can set attribute android:onClick="method_name" in xml file only.

Akshat Agarwal
  • 2,837
  • 5
  • 29
  • 49
user543
  • 3,623
  • 2
  • 16
  • 14
0

This might be what you want to avoid a mess

public class SomeActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState){
        Button button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view){
                SomeActivity.this.button1_onClick(view);
            }
        });
    }

    private void button1_onClick(View view){
        ///do stubs here
    }
}
vutran
  • 404
  • 2
  • 7
  • There are many ways to avoid even this mess. Use inner classes, let your `Activity` implement `OnClickListener`, etc. Using this way will grow unreadable when there are many more views. – nhaarman Jan 18 '14 at 12:53
0

You can set onClick property for any view in xml .So now u have no need to find and set onClick in onCreate.Now u need to define public method in activity of name u mentioned in xml . This looks constructed.

Manmohan Badaya
  • 2,326
  • 1
  • 22
  • 35
0

As mentioned in Activity Lifecycle, onCreate() is place where you perform basic application startup logic that should happen only once for the entire life of activity and onStart() is place where activity is visible to user and onResume() is place where user interact with activity mean touch or click. So good place to make click listener is onResume()

Reference: https://developer.android.com/guide/components/activities/activity-lifecycle#lc

Rohit Jakhar
  • 1,231
  • 2
  • 12
  • 21