Question

I have the following structure:

enter image description here

My problem is that whenever I go from Activities 1, 2 or 3 to SubActivities and back to the Activities, Options Menu stops appearing (Neither onCreateOptionsMenu nor onPrepareOptionsMenu). My guess is that this is because Activities are stopped when SubActivities are called, then Activites are not recreated (onCreate is not called) when I return to them.

Is there a way to force onCreateOptionsMenu to be called when Activity is resumed (in onResume)?

Update

I understand what's going on now. Problem is that Options Menu is called from TabActivity, not from the Activities under it. I need the onCreateOptionsMenu/onPrepareOptionsMenu of the Activities to run instead of TabActivity's.

Was it helpful?

Solution 2

Solved my problem. Problem is that Options Menu is called from TabActivity, not from the Activities under it. What I did was:

In my TabActivity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_view, menu);
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    menu.clear();
    inflater.inflate(R.menu.my_view, menu);
    return true;
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    return getCurrentActivity().onMenuItemSelected(featureId, item);
}

And in the Activities:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
// Do my work in the Activity
}

OTHER TIPS

i just call onCreateOptionsMenu in every activity i need it in seperatly then change the context for each activitys onCreateOptionsMenu seperatly to reflect the senario so when the activity is resumed so is its onCreateOptionsMenu.....hope that helps

I just use it in each activity I want to have menu options say if Im in activity 1 my menu options reflect activity 2,3 when I'm in activity 2 my menu options reflect 1,3 and so on but in each activity that has a menu you must put the code in doing that when you change between activities you will always have a menu.....it works for me I don't know if its the best way but it works for me

public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu);

    menu.add (Menu.NONE, 0, Menu.NONE,(""));
    menu.add(Menu.NONE, 1, Menu.NONE, (""));
    menu.add(Menu.NONE, 2, Menu.NONE,(""));
    menu.add(Menu.NONE, 3, Menu.NONE,(""));
    menu.add(Menu.NONE, 4, Menu.NONE,(""));
    menu.add(Menu.NONE, 5, Menu.NONE,(""));
    menu.add(Menu.NONE, 6, Menu.NONE,(""));
    menu.add(Menu.NONE, 7, Menu.NONE,(""));
    menu.add(Menu.NONE, 8, Menu.NONE,(""));
    menu.add(Menu.NONE, 9, Menu.NONE,(""));
    return true;
} // end onCreateOptionsMenu()

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case 0:

            return true;
        case 1:

            return true;
        case 2:


          return true;
        case 3:



             return true;
        case 4:


            return true;
        case 5:




            return true;
        case 6:

           return true;
        case 7:




            return true;
        case 8:


            return true;
        case 9:



            return true;


    }
    return false;
}

is what i use...if this helps maybe check out my question "append.text at cursor in fragment"....im super desperate for help

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top