我有一个奇怪的问题 - 我的app
是由fragments
构建的。主要想法是用户获得days
的列表以及一些计划。用户可以添加一天,添加过程结束时应再次显示日期列表(用菜单项添加另一天)。
流程如下:
dayList片段是:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initializeData(view);
}
public void initializeData(final View view){
Call<List<Days>> call = apiService.getDays();
call.enqueue(new Callback<List<Days>>() {
@Override
public void onResponse(Call<List<Days>> call, Response<List<Days>> response) {
dayRequests = response.body();
Log.d("x","SETTING DAYS COUNT TO: "+ dayRequests.size());
daysCount = dayRequests.size();
setupDayRecycler(view);
}
@Override
public void onFailure(Call<List<Days>> call, Throwable t) {
}
});
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_addday,menu);
MenuItem item = menu.findItem(R.id.action_addplan);
if (item !=null)
item.setVisible(false);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_addday) {
Bundle bundle = new Bundle();
Log.e("xxx", "DAYS COUNT IN MENU CLICK : " + daysCount);
bundle.putInt("addday", daysCount+1);
Fragment fragment = AddDayFragment.newInstance();
fragment.setArguments(bundle);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
android.R.anim.fade_out);
fragmentTransaction.replace(R.id.frame, fragment, "adbck");
fragmentTransaction.commit();
return true;
}
return super.onOptionsItemSelected(item);
}
我的AddDayFragment
结尾如下:
Fragment fragment = MyDaysFragment.newInstance();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame, fragment,"adback");
fragmentTransaction.commit();
日志输出是:
03-14 08:19:01.953 19380-19380/com.example E/x: SETTING DAYS COUNT TO: 5
03-14 08:19:06.299 19380-19380/com.example E/xxx: DAYS COUNT IN MENU CLICK : 5
03-14 08:19:11.029 19380-19380/com.example E/x: SETTING DAYS COUNT TO: 6
03-14 08:19:14.466 19380-19380/com.example E/xxx: DAYS COUNT IN MENU CLICK : 5
看起来onOptionsItemSelected
方法没有正确的值,有人可以给我一个提示吗?