Android ActivityGroup Menu Problem

后端 未结 5 1521
栀梦
栀梦 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:21

    use this code on the tabGroupActivity

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // TODO Auto-generated method stub
    
    
        return  getLocalActivityManager().getCurrentActivity().onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        // TODO Auto-generated method stub
        return getLocalActivityManager().getCurrentActivity().onMenuItemSelected(featureId, item);
    }
    

    use the code which given below in both 2 child Activities

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    
        getMenuInflater().inflate(R.menu.menu_1, menu);
        return true;
    }// menu_1 is the xml file in menu folder
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    
        switch (item.getItemId()) {
        case R.id.weekly:
            Intent weekly = new Intent(getParent(),Activity1.class);
            TabGroupActivity parentActivity = (TabGroupActivity)getParent();
            parentActivity.startChildActivity("weeklyview", weekly);
    
            break;
    
        case R.id.daily :
            Intent daily = new Intent(getParent(),Activity2.class);
            TabGroupActivity parentActivity2 = (TabGroupActivity)getParent();
            parentActivity2.startChildActivity("daily", daily);
            break;
    
        default:
            break;
        }
    
        //use return true
    return true;
    }
    
    0 讨论(0)
  • 2020-12-30 15:23

    Check this thread.

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

    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2020-12-30 15:30

    You need to override the menu methods in the activity group, and call the corresponding methods on the child activity. See this article: How to Create Options Menu on Child Activity inside an ActivityGroup

    public class TestGroup extends ActivityGroup {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            //start child activity
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            return getLocalActivityManager().getCurrentActivity().onCreateOptionsMenu(menu);
        }
    
        @Override
        public boolean onMenuItemSelected(int featureId, MenuItem item) {
            return getLocalActivityManager().getCurrentActivity().onMenuItemSelected(featureId, item);
        }
    }
    
    0 讨论(0)
  • 2020-12-30 15:34

    Yet another approach is to create the menu in the ActivityGroup root and then use public boolean onPrepareOptionsMenu(Menu menu) to clear and re-add menu items.

    In ActivityGroup class:

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        //what is the current activity?
        menu.add(0, 0, 0, "holder");
        return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu)
    {
        //start a new
        menu.clear();
        //add some menu options
        .getLocalActivityManager().getCurrentActivity().onPrepareOptionsMenu(menu);
        return super.onPrepareOptionsMenu(menu);
    }
    

    In Activity:

    @Override
    public boolean onPrepareOptionsMenu(Menu menu)
    { //add menus or inflate here
        return true;
    }
    
    0 讨论(0)
提交回复
热议问题