OptionsMenu stops appearing when navigate to other activity - android

I have the following structure:
My problem is that whenever I go from Activities 1, 2 or 3 to SubActivities and back to the Activities, Options Menu stops appearing (Neither onCreateOptionsMenu nor onPrepareOptionsMenu). My guess is that this is because Activities are stopped when SubActivities are called, then Activites are not recreated (onCreate is not called) when I return to them.
Is there a way to force onCreateOptionsMenu to be called when Activity is resumed (in onResume)?
Update
I understand what's going on now. Problem is that Options Menu is called from TabActivity, not from the Activities under it. I need the onCreateOptionsMenu/onPrepareOptionsMenu of the Activities to run instead of TabActivity's.

i just call onCreateOptionsMenu in every activity i need it in seperatly then change the context for each activitys onCreateOptionsMenu seperatly to reflect the senario so when the activity is resumed so is its onCreateOptionsMenu.....hope that helps

Solved my problem. Problem is that Options Menu is called from TabActivity, not from the Activities under it. What I did was:
In my TabActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_view, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
menu.clear();
inflater.inflate(R.menu.my_view, menu);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
return getCurrentActivity().onMenuItemSelected(featureId, item);
}
And in the Activities:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Do my work in the Activity
}

I just use it in each activity I want to have menu options say if Im in activity 1 my menu options reflect activity 2,3 when I'm in activity 2 my menu options reflect 1,3 and so on but in each activity that has a menu you must put the code in doing that when you change between activities you will always have a menu.....it works for me I don't know if its the best way but it works for me

public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add (Menu.NONE, 0, Menu.NONE,(""));
menu.add(Menu.NONE, 1, Menu.NONE, (""));
menu.add(Menu.NONE, 2, Menu.NONE,(""));
menu.add(Menu.NONE, 3, Menu.NONE,(""));
menu.add(Menu.NONE, 4, Menu.NONE,(""));
menu.add(Menu.NONE, 5, Menu.NONE,(""));
menu.add(Menu.NONE, 6, Menu.NONE,(""));
menu.add(Menu.NONE, 7, Menu.NONE,(""));
menu.add(Menu.NONE, 8, Menu.NONE,(""));
menu.add(Menu.NONE, 9, Menu.NONE,(""));
return true;
} // end onCreateOptionsMenu()
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
return true;
case 1:
return true;
case 2:
return true;
case 3:
return true;
case 4:
return true;
case 5:
return true;
case 6:
return true;
case 7:
return true;
case 8:
return true;
case 9:
return true;
}
return false;
}
is what i use...if this helps maybe check out my question "append.text at cursor in fragment"....im super desperate for help

Related

How to replace menu dynamically with another in Android

I have a ListActivity (SherlockListActivity) and its content can be dynamically changed by the user. When the content changes, the options menu should be replaced.
As I learned, instead of onCreateOptionsMenu I should use onPrepareOptionsMenu that is (supposed to be) called every time the user selects the menu.
This is called right before the menu is shown, every time it is shown.
I have the following code:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
menu.clear();
if (UserOption == UserOption1)
getSupportMenuInflater().inflate(R.menu.menu_option1, menu);
else
getSupportMenuInflater().inflate(R.menu.menu_option2, menu);
return super.onPrepareOptionsMenu(menu);
}
It is called only once during debug, so I always have the same menu.
What should I set to make it work?
Create and prepare the options menu for changing and its item selection method
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
MenuInflater inflater = getMenuInflater();
if(menuString=="red"){
inflater.inflate(R.menu.red_menu, menu);
}else if(menuString=="green"){
inflater.inflate(R.menu.green_menu, menu);
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
return true;
case R.id.help:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Like whenever you want to change menu call
String menuString="red or green";
invalidateOptionsMenu();
Like others told, if you want to have static menu use onCreateOptionsMenu, and if you want to change its visibility dynamically use onPrepareOptionsMenu along with onCreateOptionsMenu

Menu doesn't show up in android

I have the below code and I can't understand why my code doesn't show up menu. If the activity containing data, condition_true = true otherwise condition_true = false. I can clearly see the control going to menutag and the condition is true too. Even I am getting Inflated string too in log. But still menu doesn't show up. I have seen lot of posts on these like link 1, link2 etc., but that didn't solve my problem. Can someone please help me on this issue?
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear(); //Clear view of previous menu
MenuInflater inflater = getSupportMenuInflater();
Log.d("menutag", condition_true);
if(condition_true)
{
inflater.inflate(R.layout.menu, menu);
Log.d("menutag","Inflated");
}
return super.onPrepareOptionsMenu(menu); // replaced this with true, but no use.
}
You have to add the options attributes in xml.. Otherwise you can do it programmatically like this:
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.yourmenuxmlfilename, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId){
case R.id.item1:
// what you want to do with first button
break;
case .....
break;
}
return true;
}
Maybe you're overriding onKeyDown and it always returns true? This can cause the problem, because it will catch the menu button pressed event too.

option menu not clicked at first time in sherlockfragment

I have used option menu in my fragment.The problem is that when i go first time to fragment ,the option menu click event is not called.But when i go to another fragment.and again revist that fragment then the option menu click event is called...
following is the code
//Creating the option menu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.newcarmenu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
//super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case R.id.menuNewCar:
_menuClickCallback.onMenuSelected();
break;
}
return super.onOptionsItemSelected(item);
}
please tell me why this happen?
Right now, you are returning super.onOptionsItemSelected(item) every time, so the selection is being passed on. You need to return true when your MenuItem has been selected. Try this instead:
switch(item.getItemId())
{
case R.id.menuNewCar:
_menuClickCallback.onMenuSelected();
return true;
default:
return super.onOptionsItemSelected(item);
}

Android access menu from onOptionsItemSelected

I have the following in my activity (sorry new to Java/Android):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.selectItem:
// menu.add(...) --> how to get the menu instance?
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I am wondering, how can I access a menu object in onOptionsItemSelected? For example, how would I go about adding a new view to the options menu based on the selection of an existing menu item? Is the answer related to "onPrepareOptionsMenu"?
you should use SubMenu for such things ... remeber that you cant add submenu to another submenu ... so only Menu->Submenu is possible you can't do stuff like this Menu->Submenu->Submenu (while Submenu is Dialog with choices)

How to add intent on menu event

I want to call activity on menu event.I am new to android so i am trying to open new screen on delete option on menu, for this i write switch case but i didnt get how to call another activity in that please help me my code is
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.delete: Intent i= new Intent(this,deleteAct.class);
startActivity(i);break;
case R.id.edit://calling another intent; break;
case R.id.showall://calling another intent; break;
}
return true;
}
please help me ..
Neha... Did you declare the activity in the AndroidManifest.xml file as in:
<activity android:name= "deleteAct"
android:label="#string/delete_act_title">
</activity>
Also, consider adding:
default:
return super.onOptionsItemSelected(item);
JAL
Some code here.

Categories

Resources