The World’s Largest Online Community for Developers

'; java - Why do android run methods like "onItemSelected" even if they are not called in the oncreate Method? - LavOzs.Com

Hello everyone i begin to learn Android development and i do not Unterstand how it works. I understood that there is not a main. Instead it is the android lifecyle. But now i do not Unterstand why methods like "onItemSelected" or "onTouchEvent" executes by android even if tbis methods do not called.


These methods are listeners, so they are listening to the special action you make. They will be called every time you select the item or touch a UI stuff you are listening to. For example, there is a method called setOnClickListener which listens every time you click on something, e.g. any button you have. The syntaxis will be like:

final Button button = (Button) findViewById(;
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // your handler code here

It will be called on touch your button no matter where it is.

Is there a way to run Python on Android?
Fastest way to determine if an integer's square root is an integer
How to check if a service is running on Android?
How to declare global variables in Android?
Fling gesture detection on grid layout
Is quitting an application frowned upon?
How to call a method after a delay in Android
Run/install/debug Android applications over Wi-Fi?
How to avoid reverse engineering of an APK file?
Why is “final” not allowed in Java 8 interface methods?