where is the best place to put onclick listeners in android activity? I have seen two different ways. one way is to put them inside the onCreate method, and another way is to put them below and outside the onCreate method.
I just started a new android app and am confused on where is the best place.
both ways are shown below.
psudo code example one;
onclick method inside of oncreate method
MainActivity extends Activity{
onCreate(){
onClick listeners
} // end of onCreate method
} // end of MainActivity class
psudo code example two;
putting onclick below and outside oncreate method
MainActivity extends Activity{
onCreate(){
} // end of onCreate method
onClick listeners
} // end of MainActivity class
Both way are correct. How you organize the code is actually for readability’s sake. If you have too many listeners in
onCreate(), it’s harder to read the code through afterwards. However, if you just have one and it’s one liner, it’s good to put it inonCreate().