我正在尝试执行以下操作: 我有几个片段,其中有一个应该是活动工具栏中显示的编辑和删除图标。当我退出这个片段时,他们应该被删除。问题是..我实际上不知道这个问题,这就是我要求的原因。这是我的片段(我也希望你能看到我的代码并告诉我我可以改进和改变的地方,因为我认为它非常混乱):
public class ContentFragment extends Fragment {
public ContentFragment() {
// Required empty public constructor
}
TextView title;
TextView content;
TextView username;
TextView date;
TextView plus;
TextView minus;
String titlev;
String contentv;
String authorv;
String datev;
String usernamev;
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
RelativeLayout lv = (RelativeLayout) inflater.inflate(R.layout.fragment_content, container, false);
title = (TextView) lv.findViewById(R.id.titlein);
content = (TextView) lv.findViewById(R.id.contentin);
username = (TextView) lv.findViewById(R.id.usernamein);
date = (TextView) lv.findViewById(R.id.datein);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
titlev = preferences.getString("title", "");
contentv = preferences.getString("content", "");
authorv = preferences.getString("author", "");
datev = preferences.getString("date", "");
usernamev = preferences.getString("usernames", "");
System.out.println("CF: " + titlev + contentv + authorv + datev);
title.setText(titlev);
content.setText(contentv);
username.setText(authorv);
date.setText(datev);
//GET THE ACTIVITY TOOLBAR
Toolbar toolbar2 = (Toolbar) getActivity().findViewById(R.id.toolbar);
toolbar2.inflateMenu(R.menu.menu_author);
toolbar2.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.action_delete) {
System.out.println("DELETE2_______");
} else if (item.getItemId() == R.id.action_edit) {
System.out.println("EDIT2________");
}
return true;
}
});
return lv;
} }
menu_author是在调用片段时应该始终应用的xml,而menu_是用于共同活动的xml。
我尝试了以下内容:
@Override
public void onPause() {
System.out.println("HIDDEN");
Toolbar toolbar2 = (Toolbar) getActivity().findViewById(R.id.toolbar);
toolbar2.inflateMenu(R.menu.menu_);
super.onPause();
}
@Override
public void onResume() {
System.out.println("SHOWED");
Toolbar toolbar2 = (Toolbar) getActivity().findViewById(R.id.toolbar);
toolbar2.inflateMenu(R.menu.menu_author);
super.onResume();
}
但它没有用,所以我该怎么用?
感谢您的关注!