What is onCreateOptionsMenu(Menu menu)

前端 未结 4 1037
太阳男子
太阳男子 2020-12-29 08:23

What are the two parameters Menu and menu in method onCreateOptionsMenu(Menu menu) and how to use this method. I have another question why this pa

4条回答
  •  青春惊慌失措
    2020-12-29 09:01

    Menu is just the type of the parameter menu. For example you can have a String type for a variable named string, dog, etc. And in this case there's a Menu type for a parameter named menu.

    You use onCreateOptionsMenu() to specify the options menu for an activity. In this method, you can inflate your menu resource (defined in XML) into the Menu provided in the callback.

    For example:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.game_menu, menu);
        return true;
    }
    

    Fore more information, visit this link.

    As for this,

    Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called.

    For example:

    public void sendMessage() {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
    }
    

    The constructor takes two parameters and a Context as its first parameter. this represents environment data and provides global information about an application environment.

    For more information on the intent example you provided, check this out.

提交回复
热议问题