简体   繁体   中英

Mutiple menu icons generated onResume of fragments

hey guys this a function in my fragment view to dynamically add a item in my action bar.

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Inflate the menu; this adds items to the action bar if it is present.
         super.onCreateOptionsMenu(menu, inflater);
         fav = menu.add(1,1,1,"add");
         fav.setIcon(R.drawable.add_label);
         fav.setShowAsAction(2);
    }

my problem is when the device is locked and resumed the item duplicates it self in the action bar. any insight to solve the problem is appreciated. thanks

Try use

fav = menu.findItem(R.id.fav);
if(fav == null) {
    fav = menu.add(1,1,1,"add");
}

where R.id.fav is the id of your item in menu xml layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM