Implement identical Menu across several activities

Put your problem here if it does not fit any of the other categories.

Implement identical Menu across several activities

Postby akshaydashrath » Mon Dec 14, 2009 11:41 pm

Hello all,

I have a small problem. I'm writing an application with several activities and need to have identical menu in all the activities. I was wondering if its possible to share the code for the menu between these activities.

Thank you,

Akshay
akshaydashrath
Freshman
Freshman
 
Posts: 9
Joined: Mon Dec 14, 2009 11:38 pm
Location: UK

Top

Postby stifry » Tue Dec 15, 2009 5:54 pm

You can do this either by inheritance or use a utility class like I did:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         @Override
  3.  
  4.     public boolean onCreateOptionsMenu(Menu menu) {
  5.  
  6.         ActivityUtil.onCreateOptionsMenu(menu, this);
  7.  
  8.         return true;
  9.  
  10.     }
  11.  
  12.  
  13.  
  14.     @Override
  15.  
  16.     public boolean onOptionsItemSelected(MenuItem item) {
  17.  
  18.         ActivityUtil.onOptionsItemSelected(item, this);
  19.  
  20.         return true;
  21.  
  22.     }
  23.  
  24.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


In ActivityUtil, you can define your menus.

Jeff
Visit Trackaroo.com!
Trackmaster - Motorsports Lap Timer
Dynomaster - Performance Dyno
Image Image
User avatar
stifry
Developer
Developer
 
Posts: 46
Joined: Tue Feb 17, 2009 7:50 pm

Postby akshaydashrath » Thu Dec 17, 2009 11:22 pm

Hey Jeff,

Thanks, did it using the subclass approach.

Regards,

Akshay
akshaydashrath
Freshman
Freshman
 
Posts: 9
Joined: Mon Dec 14, 2009 11:38 pm
Location: UK

Postby stifry » Thu Dec 17, 2009 11:36 pm

Yeah, that works, too. The only problem with that is if you need to subclass another important class like MapActivity. Then you're kind of stuck.
Visit Trackaroo.com!
Trackmaster - Motorsports Lap Timer
Dynomaster - Performance Dyno
Image Image
User avatar
stifry
Developer
Developer
 
Posts: 46
Joined: Tue Feb 17, 2009 7:50 pm

Postby akshaydashrath » Thu Dec 17, 2009 11:40 pm

The activities I required the menu for were a subclass of a user defined activity which in turn actually was a subclass of MapActivity, so no problems there, and this basically is the core of the application, so I dont think it would change in the future, so I guess I'm safe :D. Thanks again.

Akshay
akshaydashrath
Freshman
Freshman
 
Posts: 9
Joined: Mon Dec 14, 2009 11:38 pm
Location: UK

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 11 guests