Where to set all Listeners?
Asked Answered
S

6

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?

Stefan answered 18/1, 2014 at 12:36 Comment(2)
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)Myelitis
you can use constructive alternative which i have posted among in answers.Nomination
S
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!

Sachikosachs answered 18/1, 2014 at 12:42 Comment(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 appUnshaped
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.Sachikosachs
N
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

Nertie answered 18/1, 2014 at 12:48 Comment(0)
N
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.

Nappy answered 18/1, 2014 at 12:50 Comment(0)
B
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
    }
}
Breve answered 18/1, 2014 at 12:51 Comment(1)
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.Myel
N
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.

Nomination answered 18/1, 2014 at 13:23 Comment(0)
P
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

Pyrotechnic answered 21/4, 2022 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.