What is the difference between Android constructor and onCreate ()?

I am a little confused by the difference between Java and Android Java. Let's say I have an AndroidX Activity AndroidX . There is no main function, and the AndroidX() constructor does not exist, as we know it. I understand that onCreate() most likely initializes AndroidX activity, but why not the main one? What's the difference?

+6
source share
4 answers

This graphic may help. http://developer.android.com/images/activity_lifecycle.png

In the Activity documentation, they describe in detail what each function is intended for (for example, onCreate (), onResume (), etc.). http://developer.android.com/reference/android/app/Activity.html

+1
source

Keep in mind that there are many * core * s in your activity, and your manifest directs the execution to one of them.

Also think that the constructor that we know earlier is hidden , and now it's always called onCreate ()

Honest enough to keep going?

+4
source

There is no β€œcore” because it is assumed that your application is working or not working. But on Android, there are many other possible states in which your application can be paused, stopped, started, etc.

Check out this link to get an excellent overview of the Android life cycle.

+1
source

How onCreate works is described on the Activity page of the Android Developers Guide. In particular, here:

onCreate (Bundle) is where you initialize your activity. Most importantly, here you usually call setContentView (int) with the layout resource defining your user interface, and using findViewById (int) to retrieve the widget in that user interface that you need to interact with the software.

In a sense, you can consider this method as a constructor for your activity, since initialization is performed there (see Activity Life Cycle ).

For the core, consider it hidden from you. Typically, you register for user interface elements, such as buttons or text fields, and then act on the input from these user interface elements. These listeners handle calls to your methods, which can manipulate data or change the way the user interface is displayed.

+1
source

Source: https://habr.com/ru/post/894032/


All Articles