java - Inflate different menu depending on boolean condition -


i have favourites feature users can add favourite recipes , remove them afterwards. have menu item on actionbar can perform these 2 actions. have 2 different menus this. when fragment recipe page displayed, check performed see if recipe has or has not been favourited user. if has been favourited, "delete" icon should appear, if has not, "add favourites" icon should appear.

however, when test loading favourited recipe page, "add favourites" button displayed this:

enter image description here

but when switch tab , switch back, correct delete icon displayed.

enter image description here

why not displaying correct menu on first attempt?

ingredientsfragment.java

 boolean exists;    @override public void oncreateoptionsmenu(menu menu, menuinflater inflater) {     menu.clear();     super.oncreateoptionsmenu(menu, inflater);     session = new sessionmanager(getactivity());     new checkfavourite().execute();     getexistence();      if (session.isloggedin()) {          if (exists){             inflater.inflate(r.menu.delete_from_favs, menu);         }          else  {             inflater.inflate(r.menu.add_to_favs, menu);          }      }     restoreactionbar();  }  class checkfavourite extends asynctask<string, string, string> {      private string user_id, recipe_id;      /**      * before starting background thread show progress dialog      * */     @override     protected void onpreexecute() {         super.onpreexecute();          user_id = session.getid();         recipe_id = result.getrecipeid();          system.out.println("user id: " + user_id);         system.out.println("recipe id: " + recipe_id);     }      /**      * checking favourites      * */     protected string doinbackground(string... args) {          // building parameters         hashmap<string, string> params = new hashmap<>();         params.put("user_id", user_id);         params.put("recipe_id", recipe_id);           // getting json object         jsonobject json = jsonparser.makehttprequest("mywebsiteurl",                 "post", params);          // check log cat json response         log.d("create response", json.tostring());          // check success tag         try {             int success = json.getint(tag_success);              system.out.println(success);              if (success == 1) {                 settrue();                 log.d("exist", json.tostring());              } else {                 setfalse();                 log.d("does not exist", json.tostring());              }         } catch (jsonexception e) {             e.printstacktrace();         }          return null;     }      /**      * after completing background task dismiss progress dialog      * **/     protected void onpostexecute(string file_url) {         // dismiss dialog once done       }  }  public boolean getexistence(){     return this.exists; }  public void settrue(){     exists = true; }  public void setfalse(){     exists = false; } 

add_to_favs.xml

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">  <item android:id="@+id/favourites"     android:title="@string/action_example"     android:icon="@drawable/favourites"     app:showasaction="withtext|ifroom">     </item> </menu> 

delete_from_favs.xml

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">  <item android:id="@+id/delete"     android:title="@string/action_example"     android:icon="@drawable/delete"     app:showasaction="withtext|ifroom"> </item>  </menu> 

i have tried many things putting both menu items in same menu , setting 1 or other visible/invisible. tried using onprepareoptionsmenu method resulted in same outcome. know i'm missing stupid. please help!

just use 1 menu , have both items inside it....and programatically hide/show it..

menu.xml (inside activity)

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">  <item android:id="@+id/favourites"     android:title="@string/action_example"     android:icon="@drawable/favourites"     app:showasaction="withtext|ifroom">     </item>  <item android:id="@+id/delete"     android:title="@string/action_example"     android:icon="@drawable/delete"     app:showasaction="withtext|ifroom"> </item> </menu> 

inside on create view

 @override public void oncreateoptionsmenu(menu menu, menuinflater inflater) {  menuitem fav = menu.finditem(r.id.favourites);  fav.setvisible(true);  menuitem del = menu.finditem(r.id.delete);  del.setvisible(true); }  //based on condition  if (session.isloggedin()) {          if (exists){            del.setvisibility(true);            fav.setvisibility(false);         }else{           fav.setvisibility(true);           del.setvisibility(false);       } } } 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -