Tell me, please, how are the public methods called on Android? I do not understand. If you read the literature on java, then the method should be called through an object, for example, "object.method". But in the android it is enough just to describe the public method and it will be called by itself, as indicated in the example below. Please tell me who called the onCreateOptionsMenu method? Where is the call to this method? Why did he volunteer? Where to read about it?

 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub menu.add("menu1"); menu.add("menu2"); menu.add("menu3"); menu.add("menu4"); return super.onCreateOptionsMenu(menu); } } 

    4 answers 4

    These are system callback methods (callback methods, which, as a rule, are indicated by the on- prefix in the method name). They are caused by the system upon the occurrence of certain system events.

    For example, onCreateOptionsMenu() is called by the system when the application needs to create a menu (which is called when the system menu button is pressed).

    The following happens: when the application needs to display the menu, the system starts to perform certain manipulations, in one of the stages of which the onCreateOptionsMenu() callback is triggered, here the application programmer can enter his code that will be executed along with the system one when creating the menu and which the rule determines what this menu will contain (items)

    See also this answer by system callbacks. You can also read everything that you can find sometimes the life cycle of the activation, system callbacks and any book on android development, where all this is described in some detail. I can recommend the book B. Hardy "Android. Programming for professionals" - 2016, 2nd edition

      In fact, a good question, because it is not very obvious, even if you read about the life cycle of the Activity .

      So, briefly: The first thing that happens is that an Activity object is created. At the same time, only its constructor works and nothing else:

       Activity activity = new Activity(); 

      But usually no one creates an object in this form. Instead, everything goes through the Intent :

       Intent intent = new Intent(previousActivity, Activity.class); startActivity(intent); 

      If there is no previous Activity (in this case, previousActivity ), then the system simply starts the selected Activity.class, in fact, also via intent.

      Only after startActivity(intent) onCreate(Bundle savedInstanceState) method onCreate(Bundle savedInstanceState) , i.e. in other words, it is this startActivity that startActivity launches it. In fact, the chain is slightly longer:

      startActivity -> create () -> onCreate ()

      In fact, the developer does not have access to the create () method, it can even be called differently, in any case, this method creates an Activity and starts the callback onCreate ;

      Further, as soon as onCreate works, it starts the callback , which in turn launches the start () method, it launches the callback onStart , onStart in turn, launches the callback , which launches resume () and then the callback onResume .

      As for onCreateOptionsMenu everything is not so obvious, since in different versions of Android the launch looked different. Intentionally omitting the old versions, you can say for sure that with Android 3.0+, according to the documentation, it starts at the start of the Activity, ie in the start () method in the order in which the app bar elements are displayed (usually the support bar is used as the app bar)

        Your MainActivity class is an extension of the Acrtivity class. An activity is a screen, something like a form in Windows. For example, the browser in which you are sitting. The onCreate method calls the Android system when the application starts. onCreateOptionMenu is called by some class responsible for displaying everything.

          Read about the life cycle of the Activity. As far as I understand, onCreateOptionsMenu (Menu menu) is called automatically when an Activity is created. like many other features. Check startandroid.ru/ for a lot of useful stuff.