Android: Options menu for nested Activity in Tab
I have a TabActivity which contains an Activity. When the tab f开发者_如何学编程or the activity is selected, if I press the Menu button, onPrepareOptionsMenu is called in the parent TabActivity, but not on the activity for the tab which was selected.
The options menu for the activity in the tab isn't shown unless I click inside the tab, then I get calls to both (which is what I want). Is there any way to 'focus' the activity in the tab when the tab is selected?
Just use this code for all tab activity without giving option menu for TabHost (Main Activity)
/**...........Context Menu........cg.*/
public boolean onCreateOptionsMenu (Menu menu) {
menu.add(0, 0, Menu.NONE, "Refresh");
menu.add(0, 1, Menu.NONE, "Exit");
return true;
}
/**---------- Handles item selections ------------cg */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
System.out.println("Refresh");
return true;
case 1:
System.out.println("Exit");
return true;
}
return false;
}
I hope it will help you.
Thank you, Ganapathy.
I am not sure I understand the question.
IF you want to refresh the current TAB, the one that is in focus, and the menu is from the parent (Tabs), try this:
Inside the menu you use:
Activity MyActivity = this.getCurrentActivity();
if ( MyClassActivity.class.isInstance(MyActivity) == true )
{
((MyClassActivity)MyActivity).Refresh();
}
You should have a refresh function implemented in your activity.
Hope this helps you.
Adrian.
精彩评论