Android ActivityGroup Menu Problem

后端 未结 5 1520
栀梦
栀梦 2020-12-30 14:58

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

5条回答
  •  有刺的猬
    2020-12-30 15:25

    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)

提交回复
热议问题