android - Avoid recreation of programmatically created menuitem in fragment -


my question how can prevent programmatically created toolbar menuitem (menu submenus) gets overridden xml layout every time fragment resumes. want create expensive toolbar menuitem when fragment first time created , not when resumes.

i inflat toolbar in oncreateoptionsmenu() , store instance of menu item.

private menuitem menuitem; 

private submenu expessivesubmenu;

@override public void oncreateoptionsmenu(menu menu, menuinflater inflater) {      inflater.inflate(r.menu.menu_layout, menu);     menuitem= menu.finditem(r.id.menuitem); } 

the menuitem gets populated when async loader finishs.

   @override     public void onloadfinished(loader<> loader, data data) {                  //expensive call           expensivesubmenu= makeexpensivesubmenu(menuitem, data); } 

now menu populated , visible in toolbar , have instance of submenu.

because oncreateoptionsmenu() called evertime fragment resumed menu get's overridden xml layout , have create expensive submenu again. mehtod menu.addsubmenu(menu) solve needs couldn't find one. ideas appreciated.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -