views:

66

answers:

2

I have one problem using ActivityGroup. I have two activities inside an ActivityGroup and both of them use a menu (overriding the onCreateOptionMenu and onOptionsItemSelected).

Well, the problem is that the second activity in the group doesn't show the menu when I press the menu Key. The first activity works fine showing the menu.

Any idea about this issue?

I have this code in child activity:

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    boolean result = super.onCreateOptionsMenu(menu);
    menu.add(0, MENU_REFRESH, 0, R.string.menu_refresh).setIcon(R.drawable.ic_menu_refresh);
    return result;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case MENU_REFRESH:
        Log.d(TAG,"REFRESH");
        refresh();
        return true;
    }
    return super.onOptionsItemSelected(item);
}
+1  A: 

Check this thread.

Menu's can be handled in the parent aactivity, but created in the child activity

Macarse
ok but how can i invoke a method of the child activity if i handle the menu in the parent activity?
xger86x
@xger86x: That's the issue. You should handle the menu in a child activity not in the parent one.
Macarse
@macarse ok so.... how do i do that? do you understand my question? If i have to invoke a method on the child activity i can't from the parent..
xger86x
@xger86x: What I understand is that you are placing the menu code on the parent when you should place it on the child. If this is not the case, please provide some code so it would be easier to help you. Thanks.
Macarse
i have added the code in the question. If i put the onCreateOptionMenu in parent activity, onOptionItemSelected is not invoked in child activity
xger86x
@xger86x: Cool, I guess I understand now. Place the `onCreateOptionMenu` in the child and the `onOptionItemSelected` in the parent. And I guess you can use `LocalActivityManager` to call a method from an specific activity. Try it out!
Macarse
it works placing onCreateOptionMenu in parent activity and onOptionItemSelected on child activity and using localactivitymanager to invoke the method onOptionItemSelected.Thanks!!!!!!!
xger86x
A: 

Another nice way of handling this is by using the ActivityGroup's LocalActivityManager. Get the local activity manager, get the current activity, and perform that activity's appropriate method:

public boolean onPrepareOptionsMenu(Menu menu) {
    //super.onPrepareOptionsMenu(menu);
    return getLocalActivityManager().getCurrentActivity()
        .onCreateOptionsMenu(menu);
}

public boolean onCreateOptionsMenu(Menu menu) {
    //super.onCreateOptionsMenu(menu);
    return getLocalActivityManager().getCurrentActivity()
        .onCreateOptionsMenu(menu);
}

public boolean onMenuItemSelected(int featureId, MenuItem item) {
    //super.onMenuItemSelected(featureId, item);
    return getLocalActivityManager().getCurrentActivity()
        .onMenuItemSelected(featureId, item);
}

Note: using this strategy, you must not call super.onCreateOptionsMenu from the child activity- doing so causes a stack overflow exception. I'm not sure what the purpose of calling the superclass's on* methods, as I've omitted them and have seen no negative results. (... yet)

brack