Android活动组 - 第二个活动始终显示第一个活动菜单

时间:2012-02-14 13:03:53

标签: android

我在使用ActivityGroup时遇到一个问题。我在ActivityGroup中有两个活动,它们都使用菜单(覆盖onCreateOptionMenu和onOptionsItemSelected)。

两个活动都有不同的菜单。

嗯,问题是第二个活动总是显示第一个活动菜单,

关于这个问题的任何想法?

以下是我的代码

public class myActivityGroup extends ActivityGroup {

----

    public void startChildActivity(String Id, Intent intent) {
        Window window = getLocalActivityManager().startActivity(Id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        if (window != null) {
            mIdList.add(Id);
            View view = window.getDecorView();
            history.add(view);
            setContentView(view);
        }
    }

        public void back() {
        if (history.size() > 0) {
            int lastActivityIndex = history.size() - 1;
            int lastIDIndex = mIdList.size() - 1;

            String activityId = mIdList.get(lastIDIndex);
            Log.d(TAG, "activityId:" + activityId);

            history.remove(lastActivityIndex);
            mIdList.remove(lastIDIndex);
            setContentView(history.get(history.size() - 1));
        } else {
            finish();
        }
    }


 @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        Activity current = getLocalActivityManager().getCurrentActivity();   
        return current.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        LocalActivityManager manager = getLocalActivityManager();
        Activity current = manager.getCurrentActivity();
        return current.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        LocalActivityManager manager = getLocalActivityManager();
        Activity current = manager.getCurrentActivity();
        return current.onOptionsItemSelected(item);
    }
}



public class ChildActivity1 extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

    @Override
      public boolean onOptionsItemSelected(MenuItem item) {
        //handle on menu item selected here
        return true;
    }
}



public class ChildActivity2 extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);       
    }

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

    @Override
   public boolean onOptionsItemSelected(MenuItem item) {
        //handle on menu item selected here
        return true;
    }
}

1 个答案:

答案 0 :(得分:1)

添加活动时,“当前”活动是添加的最后一个活动。我怀疑如果你与其他活动互动,那么激活选项菜单就可以了。

尝试使用String Id:

检索所需的活动
LocalActivityManager manager = getLocalActivityManager(); 
Activity a = manager.getActivity(id); 
return a.onCreateOptionsMenu(menu);