OptionsMenu does not get created and kills the Fragment - android

The problem is, that if I press the hardware button for the OptionsMenu, the app freezes and after a half second the fragment view disappears. There is no LogCat error, this is some kind of confusing me. The menu is not working in one out of six fragments. Same problem happens in every fragment.
I want to create an OptionsMenu with the following code.
The OptionsMenu is created in the activity, where the fragments get shown.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
// then it has handled the app icon touch event
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
//return super.onOptionsItemSelected(item);
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_add:
add();
return true;
case R.id.action_logoff:
Intent i = new Intent(this, Login.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
XML: main_activity_actions.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_add"
android:title="#string/action_add"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_logoff"
android:title="Ausloggen"
android:showAsAction="ifRoom"/>
</menu>
I tried switching around the return statment to true and super.onOptionsItemSelected(item); but nothing changes. The App is developed for Android 2.3

Maybe there are some problems with the hardwarebutton and Fragments in Android version < 3.0.
Here is a guide, how to implement a Actionbar with a softwarebased solution:
http://hmkcode.com/add-actionbar-to-android-2-3-x/

Try to return super.onCreateOptionsMenu(menu) instead true
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}

Related

Menu items keeps on adding on every menu button press

I have added two menu item . Both of them works good but whenever i press menu button new menu items appear beside the old one . Like below u can see
below is my menu item xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Search"
android:icon="#drawable/search_white_24dp"
android:id="#+id/searchmenu"
app:showAsAction="always">
</item>
<item
android:icon="#drawable/settings_white_24dp"
android:title="Setting"
android:id="#+id/settingmenu"
app:showAsAction="always"
/>
</menu>
and here is my code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.searchmenu:
editText = DuwaManager.openSearchBox(this,
getSupportActionBar(), "DuwaListView");
break;
case R.id.settingmenu:
break;
case R.id.home:
NavUtils.navigateUpFromSameTask(this);
break;
}
please help where i am wrong ?
Sorry this will get to long but , I have second Question
2) My second question i have another menu. xml in which there is share icon in place of search icon , But when i am trying to show on action bar with same code menu item does not show on action bar . below is my another menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:title="Share"
android:icon="#drawable/share_white"
android:id="#+id/share_tarika"
app:showAsAction="always"
/>
<item android:title="Setting"
android:icon="#drawable/settings_white_24dp"
android:id="#id/settingmenu"
app:showAsAction="always"/>
</menu>
and its code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.tarika_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.share_tarika:
break;
case R.id.settingmenu:
break;
}
return super.onOptionsItemSelected(item);
}
Try this
#Override
public void onCreateOptionsMenu(Menu menu ) {
menu.clear();// use menu.clear
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu."your current activity name ", menu);
return true;
}
use menu.clear()
The best to do is as this :
#Override
public void onCreateOptionsMenu(Menu menu ) {
getMenuInflater().inflate(R.menu."your current activity name ", menu);
return true;
}
if you have just one menu. Then set menu from Activity page and return true then no need to clear menu. and no duplicate menu will appear.
But if you use two fragment and have to change activity menu as per fragment then only need to clear menu and reset it with new one.
And if you are using onCreateOptionsMenu(Menu menu) in fragment then remove them.

Android onCreateOptionsMenu actions are not working in fragments

I know this is not new question im asking, but i tried all the solutions. Non of them worked for me:
I am using ActionBarActivity. I'm calling few fragments through main activity.
MainActivity Menu 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.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
I created a new menu class and placed a icon. In fragment oncreate i placed
setHasOptionsMenu(true);
Also placed menu.clear();
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.menu_manage_products, menu);
}
For performing action i used following code :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("onOptionsItemSelected","yes");
switch (item.getItemId()) {
case R.id.exit:
System.exit(1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
menu/menu_manage_products.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context="b2go.moshop.devblaze.moshop.view.Products.Products_Home">
<item android:id="#+id/action"
android:icon="#drawable/exit"
android:title="#string/exit"
android:orderInCategory="100"
app:showAsAction="always"/>
I also placed log message, but log was not showing. I dont know the reason why onOptionItemSelected not performing action in fragments.
Any help would be appreciated.
Well you must not use System.exit(1)
Instead Use a finish() or
Actually it would be good Idea to call onDestroy so u can release all the Object and then call a finish Method
for fragment
fragmentTransaction.remove(yourfragment).commit()

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

Android change actionbar menu when you change layouts

I am making a simple notepad app and I need help changing the ActionBar drop down menu. I want the main layout to have options for a new note and to reach settings but I need the menu to change when the user enter the note editor. I already have the menus made I just haven't found out how to switch to the different menu when I change to the different layout.
main_menu.xml (inside res/menu folder)
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/idOne"
android:icon="#drawable/ic_launcher"
android:showAsAction="ifRoom|withText"
android:title="Title 1"/>
<item
android:id="#+id/idTwo"
android:icon="#drawable/ic_launcher"
android:showAsAction="ifRoom|withText"
android:title="Title 2"/>
</menu>
In your Activity
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.idOne:
doSomething();
return true;
case R.id.idTwo:
doSomething2();
return true;
}
return super.onOptionsItemSelected(item);
}
/* Above is presented the standard way of inflating a menu on Activity creation.
* When you need to change it later then you must override this method
* (also in your Activity)
*/
public boolean onPrepareOptionsMenu (Menu menu) {
menu.clear(); //clears the current menu
if (evaluateSomeCondition)
getMenuInflater().inflate(R.menu.another_menu, menu);
else if (evaluateSomeOtherCondition)
getMenuInflater().inflate(R.menu.yet_another_menu, menu);
return super.onPrepareOptionsMenu(menu);
}

Android onCreateOptionsMenu not called when hitting the menu button

I have the problem that my application doesn't show the options menu when I hit the menu button. Debugging shows that the onCreateOptionsMenu(Menu menu) method is not called after hitting the menu button. I have another application with the same code for the menu and there it works. So now my code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.app_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.options:
Intent intent = new Intent(this, OptionsActivity.class);
startActivityForResult(intent, 1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In res -> menu -> app_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/options" android:title="#string/options" />
</menu>
I have no idea why the onCreateOptionsMenu is not called after hitting the menu button. I hope you guys can help me.
Edit: I'm not using Fragments and the onCreateOptionsMenu is really never called. Not at the start of the app and not when i'm hitting the menu button on my device.
Not sure from your post that you're using Fragments. If so, you must set menu options on by
setHasOptionMenu(true);
call this method from Fragment's onCreate() and the options menu will then be shown.
Try to add these item attributes in your app_menu.xml file:
<item
android:id="#+id/options"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/options"/>
You must declare a string for options in Strings.xml
It will call when app starts for the first time not after menu item selected.
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
if (item.getItemId() == R.id.menuitem_id) {
}
return super.onMenuItemSelected(featureId, item);
}
this method will be called after selection
In Manifest file just set target versions as below:
android:minSdkVersion="8" android:targetSdkVersion="10"

Categories

Resources