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?
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!
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
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.
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
}
}
Activity
implement OnClickListener
, etc. Using this way will grow unreadable when there are many more views. –
Myel 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.
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
© 2022 - 2024 — McMap. All rights reserved.
onCreate
and you can implement the rest in theonClick(View v)
– Myelitis