简体   繁体   English

在片段恢复时生成多个菜单图标

[英]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. 其中R.id.fav是菜单xml布局中项目的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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