I am trying to use the onPrepareOptionsMenu and when I press the menu, it does nothing!! Where am I going wrong?
Also, am I using the finish (); command right? When the user presses exit(menuX) I want them to exit the application completely, not the activity.
Thanks! :)
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater getItnow = getMenuInflater();
getItnow.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onPrepareOptionsMenu(MenuItem item){
switch (item.getItemId()){
case R.id.menuA:
startActivity(new Intent("com.abc.example.A"));
return true;
case R.id.menuB:
startActivity(new Intent("com.abc.example.B"));
return true;
case R.id.menuC:
startActivity(new Intent("com.abc.example.C"));
return true;
case R.id.menuD:
startActivity(new Intent("com.abc.example.D"));
case R.id.menuX:
finish();
}
return false;
}
}
First off all I would rather use onOptionsItemSelected then onPrepareOptionsMenu. Because as I understand you just want to press it. It is quite the same only first line is changed.
The second thing - app exit. Well, if this menu is in your main and first activity, then when you call finish() then this activity is destroyed (not exactly but the thing is that you don't see it anymore), so if app has no more activity everything is destroyed.
More advanced method for that is using instruction android.os.Process.killProcess(android.os.Process.myPid()); and what it is doing is simply sending kill signal for whole process. As far as your app won`t get bigger, I mean you will have only your activities in this process, than it is fine. Hope it will help.
When I create an options menu, I usually use these two methods:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, 1, 0, "option text");
.....
}
and
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId() == 1)
...
}
Also, with your switch statement, you are forgetting to put in break; for each option (which I notice mostly because I am notorious for doing the same).
Related
I am trying to start an activity from an options menu, but my app keeps crashing. The only error that I receive is an ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord,Intent) error in the debug window in Eclipse.
Below is the the code that I am using at the moment, but keep in mind I have tried multiple options, all of which end in the same misery, at the same piece of code - the startActivity statement (discovered by using breakpoints, since I'm not sure how to see the stack trace in the LogCat window, as described in my previous question Android/Eclipse: assistance with LogCat).
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.changescheme, menu);
menu.findItem(R.id.changeScheme).setIntent(new Intent(this, ColourActivity.class));
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
this.closeOptionsMenu();
startActivity(item.getIntent());
return true;
}
And here is the changescheme.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/changeScheme" android:title="Change Colour Scheme" android:icon="#android:drawable/ic_menu_edit"></item>
</menu>
I have also tried using a switch(item.getItemId()) statement in the onOptionsItemSelected block as opposed to the menu.findItem in the onCreateOptionsMenu block, but still no luck.
I have defined the activity in my Manifest file. I can also start the activity from a regular button, and the first time the app opens on a device, the activity is started immediately after my splash screen, and I have had no problems with either of these methods.
To me this indicates that there is nothing wrong with the ColourActivity class or its associated layout file, but there is a problem with the implementation from the options menu.
I have also implemented this same method as shown above (in code) in a different app and had no problems, so I'm am really at a loss here.
Intent you are activating should point to some target component, which is not in your case, instead you should do following:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
this.closeOptionsMenu();
Intent intent = new Intent(ActivityA.this, ColourActivity.class);
/*Here ActivityA is current Activity and ColourActivity is the target Activity.*/
startActivity(intent);
return true;
}
Try with this,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflator = getMenuInflater();
inflator.inflate(R.menu.changescheme, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.changeScheme:
Log.d("ChangeScheme", "Selected : ChangeScheme Option");
startActivity(new Intent(MainAcitivity.this, ColourActivity.class));
return true;
caseR.id.help:
Log.d("HelpMenu", "Selected : Help Option");
//Here put your code
return true;
}
}
Check this:
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.changescheme, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.changeScheme:
//start activity here
break;
case R.id.help:
//start activity here
break;
}
return true;
}
Hi adam your code seem to be perfectly fine while i am testing on my emulator, please check whether you have added the class name "ColourActivity" to your manifest file.
<activity android:name="ColourActivity"></activity>
I have solved the problem now.
It turns out the problem was not at all on the ListActivity class, it was in fact on the ColourActivity class.
I was attempting to parse a few colours in onCreate, but I had forgotten to include the # in one of the RGB colour strings, hence the crash!
Thanks heaps for everyone's help, Adam.
In my main activity I have a menu and when a menu option is selected an Intent is created and a new activity is started. When that activity completes the process should return back to the main activity and all its previous states according to the ActivityLifeCycle.
I notice that when it returns back to the main activity, nothing is accessable and the screen dims. I can only get back to what I expect when I press the menu softkey.
Has anyone experienced this issue before? Feedback would be appreciated!
Code sample below:
#Override
protected void onResume(){
super.onResume();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_settings:
return true;
case R.id.menu_decks:
Intent launchDecks = new Intent(this, stackDecks.class);
startActivity(launchDecks);
return true;
case R.id.menu_exit:
this.onDestroy();
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The code in the first post is actually correct(I have something very similar). There is a great chance the error is in the menu.xml res file, even though it inflates without any problems. Check it is fully consistent in both places, and has the same items. I finally solved it after hours of experimenting.
You seemed to be confused with the use of super.
super.m() is used to call a superclass method.
If you inherit the method with no override then
super.m() = this.m()
See your super.onDestroy
It s absoluetly useless to override a method m() just to call super.m()
See your onResume
Sometimes it is usefull to call a super clas method, it allows you to benefit from this code in a subclass. For instance here onCreateMenuOptions is overriden and your subclass can benefit from some imitialisation code for a menu.
Regards,
Stéphane
I am trying to get my webView to move back and forward by calling up the menu. Thus far I have the menu working fine and opening, and the refresh button will refresh the browser. However, I cannot find documentation anywhere about the methods for forward of back. If the default internet browser has them by just pressing the menu key there must be out there. I just can't find them/don't know how to implement them. Any help would be beneficial, thank you.
Here is what I am trying to do:
WebView.Java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_navigation, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.back_button:
"???"
case R.id.forward_button:
"???"
case R.id.refresh_button:
refreshBrowser();
return true;
}
return false;
}
The part that I'm missing is marked by the three question marks "???". Let me know if you need more information to get some help, and thanks again.
Use webView.canGoBack and webView.canGoForward to check if there is history data to go back / forward and then load that url.
While extending a sample Android activity that fires some other activities from its menu, I came to have some menu items handled within onOptionsItemSelected, and some menu items (that just fired intents) handled by calling setIntent within onCreateOptionsMenu.
Basically something like:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_ID_1, Menu.NONE, R.string.menu_text_1);
menu.add(0, MENU_ID_2, Menu.NONE, R.string.menu_text_2);
menu.add(0, MENU_ID_3, Menu.NONE, R.string.menu_text_3).
setIntent(new Intent(this, MyActivity_3.class));
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case (MENU_ID_1):
// Process menu command 1 ...
return true;
case (MENU_ID_2):
// Process menu command 2 ...
// E.g. also fire Intent for MyActivity_2
return true;
default:
return false;
}
}
Apparently, in this situation the Intent set on MENU_ID_3 is never fired, or anyway the related activity is never started.
Android javadoc at some point goes like <<[if you set an intent on a menu item] and nothing else handles the item, then the default behavior will be to [start the activity with the intent]>>.
What does it actually mean "and nothing else handles the item"?
Is it enough to return false from onOptionsItemSelected?
I also tried not to call super.onOptionsItemSelected(item) at the beginning and only invoke it in the default switch case, but I had same results.
Does anyone have any suggestion?
Does Android allow to mix the two type of handling?
Thanks for your time everyone.
Ok. The solution was dumb enough. The destination activity name (say MyActivity_3 in the example) was mispelled in the manifest.
I changed the 3rd menu item handling to the classic switch logic in onOptionsItemSelected and I got an ActivityNotFoundException exception in Eclipse debugger.
By handling the menu item in the "setIntent way", no exception was thrown. Although if I looked at the LogCat, I could have spotted a MenuItemImpl: Can't find activity to handle intent; ignoring.
My problem is, that I have got a TabActivity, which has 4 Tabs right now. The first Tab is a special Details-Tab, where the user could modify some data.
The problem is, that if I add a OptionsMenu for the Activity, that the OptionsMenu is appearing on every Tab.
I tried to check the current mTabHost.getCurrentTabTag() in the onCreateOptionsMenu but that changed nothing.
So, how to do that?
(The following code, which still shows the OptionsMenu on every Tab)
public boolean onCreateOptionsMenu(Menu menu) {
if(mTabHost.getCurrentTabTag()==getString(R.string.tab_details)) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, EDIT_ID, 0, R.string.menu_edit).setIcon(R.drawable.edit);
return result;
}
return true;
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case EDIT_ID: {
Toast.makeText(this, "o.O", Toast.LENGTH_LONG).show();
}
}
return super.onMenuItemSelected(featureId, item);
}
I see that you are comparing strings (tags in fact) with ==, usually, this is nicer to do that with .equals() method. Maybe it will solve your issue.
To dynamically update an option menu (from the documentation) :
If you want to change the Options
Menu each time it opens, you must
override the onPrepareOptionsMenu()