Android - Sharing an options menu between activities

Say you have an Android app with multiple activities (or "screens", if you prefer...) and you want to have the same options menu appear on every activity.

Simply create another activity that implements the onCreateOptionsMenu() and onOptionsItemSelected() methods, like this:

public class MasterActivity extends Activity {

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {  
      ... whatever ...
   }
 
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {  
      ... whatever ...
   }
}

Then have all of your other classes (activities) extend your MasterActivity class:

public class MyClass extends MasterActivity {
 ...
}

Print | posted @ Wednesday, December 1, 2010 3:26 PM

Comments on this entry:

Gravatar # re: Android - Sharing an options menu between activities
by Shripad Bhat at 10/26/2011 12:32 PM

Awesome, good logic it really helps...
Post A Comment
Title:
Name:
Email:
Comment:
Verification: