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?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
From here: http://developer.android.com/reference/android/app/Activity.html
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 inonStart
oronResume
, but you should understand, that its bad practice, becauseonStart
andonResume
calls every time, when user see your activity.onCreate
calls only when Activity is initialized. It is reason, why you should useonCreate
. Actually, good practice implement method likeinitListeners()
where you can put all you listeners logic.Good luck!
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.
Use
onCreate
method to set theUI
and to get theWidget
fromUI
.And you can define a
clickListener
for the widgets and use it inonCreate
methodand you can set the above
clickListener
to a widget which you have created inonCreate
methodYou 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.
This might be what you want to avoid a mess