How Can We Use Menu Items Outside Onoptionitemselected Like In Oncreate()
I am facing a problem in getting a reference to a menu item outside onOptionsItemSelected() method. I want that part of code to be executed as soon as my Activity is created, so I
Solution 1:
Its not advisable to do what you want. While you can create a class field
private Menumenu;
and capture its value in onCreateOptionsMenu()
this.menu = menu;
it is known that the value of menu
abruptly becomes null
at certain points in the Activity
/ Fragment
life-cycle. Hence you are advised to do everything you want with menu
in the onCreateOptionsMenu()
, onPrepareOptionsMenu()
and onOptionsItemSelected()
methods. Those methods are the right place to do that.
References:
1.Menus.
Post a Comment for "How Can We Use Menu Items Outside Onoptionitemselected Like In Oncreate()"