I want to know if there's any way in Android to programatically know if
public boolean onCreateOptionsMenu(Menu menu)
has been called and returned.
I'm using actionsherlockbar library in a project and the app has to download some content and I'd like to start the download when the action bar is loaded so I can use its integrated progress interface, but I can't find a way since onCreateOptionsMenu is called after onCreate.
To sum up, hat I want to do is:
-load the actionBar
-notice it is loaded
-enable the progress bar
-download
-disable the progress bar
But I don't know how to notice it's loaded.
Thanks in advance for your help.
use Log.d() method to register in the logcat. eg:
in your case:
where you have defined the onCreateOptionsMenu() add this command:
Log.d("tag of your choice", "value you want to check if it has been initialized");
You can see more about this function in eclipse or https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CCwQFjAA&url=http%3A%2F%2Fdeveloper.android.com%2Freference%2Fandroid%2Futil%2FLog.html&ei=3RIfUqaCGcS4rgf19ICQCg&usg=AFQjCNEd4hTMzjI2BnTS2woBfjvrddoQsw&sig2=4JDVD0JjIjx0mEhog9plPQ&bvm=bv.51495398,d.bmk
Related
I have created with Android Studio 1.4 a Blank Activity where I can set menu's items to diplay.
My problem is that when I create a new activity "MainActivity2" I see the Action Bar with its colours but I'm not able to "connect" a menu with its elements. By default "onOptionsItemSelected" and "onCreateOptionsMenu" are not created automatically in this new activity.
Thanks
First activity,
MainActivity2
I found the solution. I saw some tutorials in Internet saying that to change activity I have to use "setContentView(R.layout.activity_main2)". By the way using this the application will override only the design and will never be executed the "onCreateOptionsMenu" where menu items are going to be added. The correct action to execute on the button listener is " startActivity(new Intent(MainActivity.this, Main2Activity.class)) ", so that the code can be executed.
So I created a new project and the following came up as part of the default start code.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.phone_start_screen, menu);
return true;
}
Coincidently I am trying to learn Android coding and there are parts similar to what I'm currently reading, fragments. However I do no understand the code above, I do not even know when it is being called and well I do not understand fragments but thats another matter and I will continue trying to learn that. At the moment I would just like an explanation on the code above.
onCreateOptionsMenu usually gets called when the user presses the Menu button. In the case of an action bar presence though, the method will actually get called before onCreate finishes.
As to what code does, it inflates the phone_start_screen.xml (in your example) from the /res/menu folder.
I just started using Juno, and I was wondering what this method is really about. And since I
dont use it, I dont want it to create every time i create a project. How can I disable her?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
All this does is create your menu that shows up when you press the menu key. It may also show up in the action bar depending on how your app is set up and the API version it is running on. It is not needed unless you have extra options that you want to show to the user in this manner.
This method is by default created from the ADT Activity templates.
How can we handle the check button in the CAB menu?
Is the item associated to some internal id like android.R.id.checkbox?
I've read that it could be handled in SherlockActionBar CAB, but could it be the same with the native ActionBar CAB?
Is there any way to detect the interaction of this item? onActionModeFinished() is not sufficient since I'm calling it multiple times since the CAB needs to be present due to previous changes that happened.
Thanks.
Ok, finally found a solution after trying some things.
Place the code you want for the checkbox or onbackpressed in the method below
public void onDestroyActionMode(ActionMode mode)
{
//place the code you want for the checkbox or back icon here. If you don't want
// this code run if other selections are used, then just create a boolean value that
//you earlier on and check the value in this section before implementing the code
}
};
I am using tabs, with Activity groups in each. I want to add an options menu to certain activities but it won't show on any. I have options menus working in other projects with the exact same code, so I can't figure out why they won't show up. This is the code I am using:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Option 1");
menu.add("Option 2");
menu.add("Option 3");
return super.onCreateOptionsMenu(menu);
}
Is there anything wrong with this code or anywhere else I should be looking at that might be blocking this menu from showing when I hit the menu button?
The only thing that would be blocking the menu AFAIK would be if you are overriding onKeyDown. Make sure you aren't overriding that method and thus preventing the menu button from doing what it is supposed to.