Should I place event listeners on the onCreate method or on the onResume method?

Issue

I’m a begginer to android development, and I’m trying to add an event listener to a button without using an onClick() in the xml file. Where should I place my event listener and should I remove it somewhere, say, on the onStop method?

Thanks

Solution

According to the official documentation of Android, listeners should be added in the OnCreate():
https://developer.android.com/develop/ui/views/touch-and-input/input-events

Also, it is not necessary to remove the listeners, since they no longer have a reference when the object is disposed and are thus removed.

Answered By – WannaBe

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published