Android setting share option visibility on fragments managed by tabs in sliding tab layout -
i have been struggling implement switching visibility of share option on menu between fragments. using sliding tab layout , has fragment in each of 2 tabs. first tab (uses list view) , when item selected, setting flag true , calls invalidateoptionsmenu() , works fine showing share option on app bar menu, not able cancel when move other fragment has preferences. code similar below.
public void setsharedintenttext(string text) { sharedtext = text; if (shareoptionvisibility == false) { shareoptionvisibility = true; invalidateoptionsmenu(); } public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); // locate menuitem shareactionprovider menuitem shareitem = menu.finditem(r.id.menu_share); // fetch , store shareactionprovider mshareactionprovider = (shareactionprovider) menuitemcompat.getactionprovider(shareitem); sendintent.setaction(intent.action_send); sendintent.settype("text/plain"); sendintent.putextra(intent.extra_text, sharedtext); mshareactionprovider.setshareintent(sendintent); shareitem.setvisible(shareoptionvisibility); when switch between tabs, visibility should set appropriately. tried set onpause method of first fragment, , onresume method of second fragment, control doesn't seem go these methods when @ logcat. code have used set visibility false below in fragment2.
public void onresume() { super.onresume(); log.i(tag, "on resume "); ((mainactivity) getactivity()).shareoptionvisibility = false; ((mainactivity) getactivity()).invalidateoptionsmenu(); } so know best place put code control visibility when switch between tabs. here list of classes use. mainactivity, fragment1, fragment2, slidingtablayout, slidingtabstrip , viewpageradaptor. code implemented on top of com.example.android.actionbarcompat.shareactionprovider example.
thanks in advance
issue resolved implementing oncreateoptionsmenu method fragment rather mainactivity. thanks
Comments
Post a Comment