Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onCreateOptionsMenu called before onCreate android

I am beginner to android and i am developing small android application. In my application I am using Sherlock library. My application contains one main activity and two fragments. On of my list fragment structure 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;

    }

    /*@Override
    public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);

    optionsMenu = menu;
    }*/

    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); // set the adapter

        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {
                displayCards(pos);
                Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside on option selected");
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                displayCards(0);
                Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside on no option selection");
            }

        });


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside option select listener");

    }

}

Now my problem is that when I switch fro one fragment to another fragment for first time it gives following 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

That mean on create option called before onfragment create and on activity create . and it also giving two warning. This work with out any error. But I am not able to display my list view

Now when I again switch to same fragment(for second time) it gives following 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's not giving any warning and working properly. Also displaying my list view. But for the first time it's not working properly.. Another point is that when I ran it on higher version of android its working fine. But If i ran it on lower version of android like 2.3.3 i shows such behavior.. I read about this ListFragment onPrepareOptionsMenu called before onCreate. Why and How to Fix / Bypass? But i am not able to solve this problem.

if I define setHasOptionsMenu(true) inside onActivityCreated it gives 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 to set setHasOptionsMenu for my fragment where actual fragment transaction takes place like 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 still it giving same problem.

any problem with my code or some thing else? So how to solve this problem. Need help... thank you...

like image 784
nilkash Avatar asked Feb 07 '13 05:02

nilkash


People also ask

Is onResume called after onCreate?

onResume() will never be called before onCreate() . Show activity on this post. onResume() will always be called when the activity goes into foreground, but it will never be executed before onCreate() .

Which activity is called first in Android?

in short onCreate is called first and when you comeback from an activity onResume will be called. onResume will also be called the first time as well.

Which method is called before activity is destroyed in Android?

The onDestroy() method runs immediately before the activity is destroyed. The onDestroy() method enables you to perform any final cleanup such as freeing up resources.

What is onCreateOptionsMenu in Android?

onCreateOptionsMenu() is called by the Android runtime when it need to create the option menu.


1 Answers

write setHasOptionsMenu(true); in onActivityCreated(Bundle savedInstanceState) method.

like image 152
Dhaval Avatar answered Oct 11 '22 19:10

Dhaval