Question

I have used a technique (http://united-coders.com/nico-heid/use-android-activitygroup-within-tabhost-to-show-different-activity) to develop an app where I have 3 tabs and each tab has its own ActivityGroup. I have menus for each activity. But when I press menu button, the menu does not appear. After doing some random trails I found that If I implement onCreateOptionsMenu in ActivityGroup then only menu appears. I am not able to execute onCreateOptionsMenu of Activity. Please suggest how to use menu of Activity as I have many activities in single ActivityGroup and by implementing onCreateOptionsMenu in ActivityGroup is not the right way to handle this problem.

Was it helpful?

Solution

Here is how you roll with it: In your ActivityGroup class onCreateOptionMenu() call the current Activity 's onCreateOptionMenu() i.e

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

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

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

and in your individual Activity

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
    return true;
}
@Override
public boolean onOptionsItemSelected (MenuItem item)
{
    switch (item.getItemId())
    {
    case R.id.MENU_LOGOUT:
        Dialog.showToast(this, "message");
        return true;
    case R.id.MENU_HELP:
        break;
    case R.id.MENU_ABOUT:
        break;
    }
    return super.onOptionsItemSelected(item);
}

and if you want any Activity without having any Menu just override these methods

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
    return true;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top