Reputation: 5410
I am trying to implement an options menu for my app and the same menu is used in different activities. In the Android developers site, it says the following:
Tip: If your application contains multiple activities and some of them provide the same options menu, consider creating an activity that implements nothing except the onCreateOptionsMenu() and onOptionsItemSelected() methods. Then extend this class for each activity that should share the same options menu. This way, you can manage one set of code for handling menu actions and each descendant class inherits the menu behaviors. If you want to add menu items to one of the descendant activities, override onCreateOptionsMenu() in that activity. Call super.onCreateOptionsMenu(menu) so the original menu items are created, then add new menu items with menu.add(). You can also override the super class's behavior for individual menu items.
My activities extend from Activity, ListActivity or MapActivity, so what would be the correct way to implement what they are suggesting here? is it possible? Because I cannot extend this new class for all of these, I could only do something like public abstract BaseMenu extends Activity (as explained in this question) but this doesn't work for me. So I am wondering if there is a work around I can implement.
Thanks in advance
Upvotes: 4
Views: 1539
Reputation: 3817
I would create a static MenuProvider class that implements your onCreateOptionsMenu() and onOptionItemsSelected() methods statically to be called from onCreateOptionsMenu() and onOptionsItemsSelected() of the activities, like so:
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 would 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 */);
}
...
}
Upvotes: 0
Reputation: 8079
you cannot use mapview without extending mapactivity class.... refer this.. MapView without MapActivity ..so you should let your base class extend map activity... and for activity using listview.. put listview in xml and you can use it in your activity..
Upvotes: 0
Reputation: 56925
For that common Base menu class you need to extend MapActivity class . So you can extend that common base menu class for you all activities.
For that ListActivity you can also implement the list without ListActivity, you can implement it by only Activity or MapActivity.
you have to declare you listview in xml file with the id like below.
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Then you have to declare it in your activity class .
ListView listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(your adapter);
Like above you can implement it without extend ListActivity.
Upvotes: 2