简体   繁体   中英

Implementing option menu one time for several activities

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

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.

你不能在没有扩展mapactivity类的情况下使用mapview ....请参考这个.. 没有MapActivity的MapView ..所以你应该让你的基类扩展地图活动...和使用listview的活动..把listview放在xml中你可以使用它在你的活动..

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 */);
    }

    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM