gpt4 book ai didi

android - Fragment 的菜单在屏幕旋转时将自身多板化

转载 作者:行者123 更新时间:2023-11-30 03:07:41 26 4
gpt4 key购买 nike

我有一个 fragment ,它有一个包含一个项目的菜单。问题是在每次屏幕方向更改时,此项目都会加倍。所以第一次屏幕更改后有 2 个(相同的)项目,第二次更改后有 3 个项目,依此类推...

public class ManageLinksFragment extends Fragment {



FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;



DBHttpLinks db;
long itemPosition;
SimpleCursorAdapter adapter;
ListView lv;
Listener mListener;



public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

setRetainInstance(true);
setHasOptionsMenu(true);

// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.chooseonelinkfragment, container, false);

lv = (ListView) v.findViewById(R.id.listView1);



return v;
}




@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_links, menu);
super.onCreateOptionsMenu(menu,inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
case R.id.addlinks:

fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();

// AddNewRecordFragment is a class name!
AddHttpLinksFragment fragment2 = new AddHttpLinksFragment();
fragmentTransaction.replace(R.id.fragment_left, fragment2, "addlinksF");
fragmentTransaction.addToBackStack("chooselinksF");
fragmentTransaction.commit();

return true;


default:
return super.onOptionsItemSelected(item);
}
}



@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.context_menu_manage_links, menu);
}

}

最佳答案

尝试移动:

setHasOptionsMenu(true);
setRetainInstance(true);

到你的 fragment onCreate,

另外,请确保您没有在 Activity.onCreate 中添加您的 fragment ,即使它已经存在。

关于android - Fragment 的菜单在屏幕旋转时将自身多板化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21524550/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com