I'm new to Android Development and I have a question:

In which method of the Android Activity LifeCycle must I put the event of Views, for example when I click a button what happens.

Would it be in onCreate or in onResume and why?

有帮助吗?

解决方案

In most of the cases you should have that in onCreate because this method is called once and you probably want to set those actions listeners once. onResume, according to Activity life-cycle may be called several times, according to Activity life cycle doc, and you would set those action listeners again for no reason.

However, if you would like to have a customizable behavior for that action listener for each time your Activity becomes visible again, it may worth set your action listeners in onResume, but that would probably apply for a small set of controls and I guess that would be very rare.

其他提示

May this help you:

You should intialize your variables and write your onClick events in onCreate() Method because it is called prior to all the methods in the LifeCycle of Android and it is called once in LifeCycle..

For More information Refer this link: Click Here

Go through this article

As you said in java we call the function in main method .

In android if java class extends activity you can call the method in Any of the life cycle event of activity , It depends on your need . For example if you want to call method when you get into activity follow this

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main)//To set up ui for activity

    getDispalay();
}

public void getDispalay(){
 //your actions
}

Aslo you can call methods in other life-cycles Read this

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top