user1302569
user1302569

Reputation: 7191

Action bar from main activity to second

I have a problem with action bar. I have a action bar in main activity which is creating in onCreateOptionsMenu(Menu menu). Items I am adding in this method too. Now I want to that when I click on my item in second activity was the same action bar. I want that when I copy and paste code from this method to method in second activity it work but I don't want to copy this code because I add a lot of items. This is possible to use action bar from first activity in second activity?

Upvotes: 0

Views: 2065

Answers (1)

vRallev
vRallev

Reputation: 5040

Yes, it is possible. I have three possible solutions:

  1. Add the action items via xml http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems
  2. Your second activity could extend your MainActivity (or better, create an abstract base activity for both, which only creates the menu).
  3. Extract the menu creation in a helper class and call the same method from each activity.

All three ways are possible, but I would prefer the first one.

Upvotes: 2

Related Questions