tags:

views:

96

answers:

1

Hello, I'm new to android and I'm trying to figure out if there is a best practise in menu handling. Well, here is the thing:

I have created a menu.xml file (wihtin res/menu), Main.java handle the menu action with a switch case.

I'm wondering what is the best way to run appropriate task when an action is performed on a menu item:

  • Use an intent and trigger the corresponding activity
  • define everything (which could be a lot of code) within the case corresponding to this menu item.

thanks a lot for your help, Luc

+2  A: 
...
startActivity(new Intent(getApplicationContext(),MyOtherActivity.class));
return;

it doesn't have to be more complicated than that.

Will
Thanks, that's great. By the way, I guess the method getApplicationContext() available within the whole application, this can be use in helper class or is this equivalent to pass a context on and on ? (thinking about memory leak) ?Thanks a lot, Luc
Luc
You actually don't need to call getApplicationContext(). That constructor just wants any Context for your own package so it can get the package name. Thus, if you are calling startActivity() from inside of an Activity, you can just pass in "this" since an Activity is-a Context. From a View, use getContext() to get the Context the view is running in.
hackbod