I am starting an android and I am developing a small Android application. In my application, I use the Sherlock library. My application contains one main action and two fragments. In my list fragment structure it looks like
public class MyCards extends SherlockListFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside fragment on create"); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside on activity created"); } private void displayCards(int type) { Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside display cards"); } @Override public void onResume() { super.onResume(); } @Override public void onPause() { super.onPause(); } @Override public void onDestroy() { super.onDestroy(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.mycards, container, false); View view1 = inflater.inflate(R.layout.empty_card, container, false); return view; } public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_type2, menu); Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside on create option menu"); Spinner spinner = (Spinner) menu.findItem(R.id.cardType) .getActionView(); SpinnerAdapter adapter = ArrayAdapter.createFromResource(getSherlockActivity().getSupportActionBar().getThemedContext(), R.array.card_action_list,android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter);
Now my problem is that when I first switch one fragment to another fragment it gives the next log output
01-04 00:54:06.997: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(2575): inside on create option menu 01-04 00:54:06.997: W/KeyCharacterMap(2575): No keyboard for id -1 01-04 00:54:06.997: W/KeyCharacterMap(2575): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 01-04 00:54:07.020: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(2575): inside fragment on create 01-04 00:54:07.036: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(2575): inside on activity created
This means that for the create parameter created before the onfragment was created and the activity was created. and he also gives two warnings. This job fixes any error. But I can not display my list view
Now, when I switch back to the same fragment (second time), it gives the next log output
02-07 10:05:10.983: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(15844): inside on create option menu 02-07 10:05:11.023: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(15844): inside fragment on create 02-07 10:05:11.043: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(15844): inside on activity created 02-07 10:05:11.113: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(15844): inside display cards 02-07 10:05:11.133: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(15844): inside on option selected
this time it does not warn and does not work properly. Also displays my list. But for the first time it does not work properly .. Another thing is that when I ran it on a higher version of Android, it worked fine. But if I run it on a lower version of Android, like 2.3.3, I show this behavior .. I read about this ListFragment onPrepareOptionsMenu, which is called before onCreate. Why and how to fix / work around? But I can not solve this problem.
if I define setHasOptionsMenu(true) inside onActivityCreated , it gives the following output
01-04 03:32:38.622: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(8157): inside fragment on create 01-04 03:32:38.653: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(8157): inside on activity created 01-04 03:32:38.692: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(8157): inside on create option menu 01-04 03:32:38.700: W/KeyCharacterMap(8157): No keyboard for id -1 01-04 03:32:38.700: W/KeyCharacterMap(8157): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
I also tried setting setHasOptionsMenu for my fragment, where the actual fragment transaction occurs, as shown below.
@Override public void onTabSelected(Tab tab, FragmentTransaction ft) { MyCards mc = new MyCards(); mc.setHasOptionsMenu(true); ft = mActivity.getSupportFragmentManager().beginTransaction(); mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs); ft.add(android.R.id.content, mFragment, mTag); ft.commit(); }
and yet it gives the same problem.
any problem with my code or something else? So how to solve this problem. Need help ... thanks ...