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()
Related
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.
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);
}
I'm using ActionBarSherlock in my project and sometimes need to add one or more items inside the action bar.
At this BaixadosFragment class (that extends SherlockFragment), I'm using the following code and it works fine:
#Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater)
{
inflater.inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu, inflater);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
refresh();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In this case, I'm adding a refresh button, witch is lonely inside main.xml
BUT I want to do the same at CupomDetalheActivity (though adding a share button), witch extends SherlockFragmentActivity instead. So I'm not able to override "onCreateOptionsMenu" as it has a different signature (below):
//this is inside SherlockFragmentActivity
public final boolean onCreateOptionsMenu(android.view.Menu menu) {
return true;
}
//this is inside SherlockFragment
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//Nothing to see here.
}
Whith SherlockFragmentActivity, I don't even see where can I use the inflater to bring up the xml containing the share button...
I appreciate a lot any ideas and suggestions...
[EDIT] This worked, according to DroidT's suggestion:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.share, menu);
super.onCreateOptionsMenu(menu);
return true;
}
Your SherlockFragmentActivity also has an onCreateOptionsMenu() and onPrepareOptionsMenu(). You can inflate your menu options in the onCreateOptionsMenu() by using getSupportMenuInflater(). You would want to make a call to invalidateOptionsMenu() in your SherlockFragmentActivity whenever you want the change to happen and add the menu options in onPrepareOptionsMenu(). For more information look at the "Changing menu items at runtime" section of this link.
If you are using a menu inside of a fragment, make sure you call setHasOptionsMenu(true); in the fragments onCreate(Bundle savedInstance) method
I have a master detail view, with both the master and detail fragments with their own action bar menu items; the master has some and the detail has some, but the details action bar item don't call onOptionsItemSelected when clicked. This problem is on tablet.
On the other hand if the same code is run on a phone emulator the action bar items of the detail view work with no problem.
menu.xml
<item
android:id="#+id/save_menu"
android:icon="#drawable/ic_checkmark_holo_light"
android:showAsAction="always|withText"
android:title="Save">
</item>
<item
android:id="#+id/cancel_menu"
android:icon="#drawable/ic_menu_close_clear_cancel"
android:showAsAction="always|withText"
android:title="Cancel">
</item>
inflater is working fine and both the fragments in master detail view have the setHasOptionsMenu(true); in their onCreate method.
EDIT
onCreateOptionsMenu in the fragment activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
onCreateOptionsMenu in the master fragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.itemlistactivity_menu, menu);
}
onCreateOptionsMenu in the detail fragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.addfragment_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
System.out.println("onCreateOptionsMenu called");
}
I got the solution from this link.
It says that we can achieve the desired behaviour by not intercepting the same event in Fragment Activity (Parent Activity of Fragment).
This link shows two ways however i get my thing done by using following in FragmentActivity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
return false;
}
Try it out.
i have a menu that contains just one item.
Button exit;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater blowUp = getMenuInflater();
blowUp.inflate(R.menu.exitmenu, menu);
return true;
}
exit=(Button)findViewById(R.id.bexitMenuExit);
if i add listener to exit button , i got excpetion (null pointer), i am sure that there is no syntax error, the button exit is comming from this menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/bexitMenuExit"
android:title="Exit"
android:icon="#android:drawable/ic_menu_close_clear_cancel"
></item>
</menu>
what am i doing wrong?
There is no need to initialize Button and this doesn't make sence for me.
Just inflate your Menu and just override onOptionsItemSelected method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(getApplicationContext()).inflate(R.menu.exitmenu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getId()) {
case R.id.bexitMenuExit:
// work that will start when you click on this
...
}
}
Also, there is no need to use OnClickListener, for this there is onOptionsItemSelected method and you should use it.
for menu items, as stated by Sajmon, you can use onOptionsIemsSelected function. onClickListener is used by views while this function has been designed to be used specifically by menu items using switch case.