Embed an options menu once for multiple actions

I am trying to implement an options menu for my application, and the same menu is used in different actions. The Android developer site says the following:

Tip. If your application contains several actions, and some of them provide the same options menu, consider creating nothing but realizes onCreateOptionsMenu () and onOptionsItemSelected (). Then expand this class for everyone who should use the same options menu. Thus, you can control one set of code to handle the actions of the menu and each descendant class inherits the behavior of the menu. If you want to add menu items to one of the child operations, overrides onCreateOptionsMenu () in this activity. Call super.onCreateOptionsMenu (menu) so that the original menu items are created, then new menu items are added using menu.add (). You can also override superclass behavior for individual menu items.

My actions apply to Activity, ListActivity or MapActivity, so what would be the right way to implement what they offer here? Is it possible? Since I cannot extend this new class to all of these, I could only do something like a public paragraph. BaseMenu extends Activity (as described in this question ), but this does not work for me. Therefore, I wonder if there is work that I can implement.

Thanks in advance

+5
source share
3 answers

For this general base menu class, you need to extend the MapActivity class. Thus, you can expand this common base menu class for all activities.

ListActivity ListActivity, Activity MapActivity.

xml , .

 <ListView
            android:id="@+id/listView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>

.

ListView listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(your adapter);

, ListActivity.

+2

mapview mapactivity.... . .. MapView MapActivity.. ... listview.. put listview xml, ..

+1

MenuProvider, onCreateOptionsMenu() onOptionItemsSelected(), onCreateOptionsMenu() onOptionsItemsSelected() , :

public static class MenuProvider {

    // You can pass it the activity and other variables used by this method if 
    //   you need to.
    // Since the implementation is the same across all activities, they should
    //   pass the same variables. 
    public static void onCreateOptionsMenu(MenuItem item, Activity callingActivity, ...)  
       ... // Do stuff on create
    }

    public static void onOptionItemsSelected(MenuItem item, Activity callingActivity, ...)  
       ... // Do stuff on item select
    }
}

And in each of your activities you will do:

public class MyMapActivity extends MapActivity {
    ...

    public void onCreateOptionsMenu(MenuItem item)  
       MenuProvider.onCreateOptionsMenu(item, this, ... /*other variables */);
    }

    public static void onOptionItemsSelected(MenuItem item)  
       MenuProvider.onOptionItemsSelected(item, this, ... /*other variables */);
    }

    ...
}
0
source

All Articles