Menu items always getting cleared - android

I have a viewpager with some fragments.
For example I have 2 fragments with different menu items.
Fragment A (menu)
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main, menu);
manager = (SearchManager) getActivity().getSystemService(getActivity().getApplicationContext().SEARCH_SERVICE);
sv = (SearchView)menu.findItem(R.id.search).getActionView();
sv.setSearchableInfo(manager.getSearchableInfo(getActivity().getComponentName()));
sv.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
pAdapter.filter(newText.toLowerCase());
return true;
}
});
}
Fragment B (no menu , clear)
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
super.onCreateOptionsMenu(menu, inflater);
}
However when launching the menu items are gone. If I comment menu.clear(); menu items appears but all the fragments have the same menu items..
For those two fragments I call in onCreate setHasOptionsMenu(true);
How can I use different menu layouts with different fragments ?
Thanks in advance

I think you should use
inflater.inflate(R.menu.main, menu);
with different menu in every fragment.

Related

building menus in fragments in android

I am building an application in which each fragments have different menus but the code I am using to add menu but the menu option doesn't actually exists.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu,menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemThatWasClickedId = item.getItemId();
if (itemThatWasClickedId == R.id.logoutitem) {
((tailorDashboard)getActivity()).logoutwork();
return true;
}
return super.onOptionsItemSelected(item);
}
sir basically i am using this code to make a option menu in fragments i have used android.support.v7.widget.toolbar option to make my custom toolbar. but the code i have already used is not actually making the option menu.
`#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu,menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemThatWasClickedId = item.getItemId();
if (itemThatWasClickedId == R.id.logoutitem) {
((tailorDashboard)getActivity()).logoutwork();
return true;
}
return super.onOptionsItemSelected(item);
}'

Change actionbar menu in fragment

I want to load another menu xml when I load the fragment.I am using this code in main activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
I am using this code in fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
getActivity().getMenuInflater().inflate(R.menu.fragment_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
When user loads the fragment,activity menu should remove then fragment menu should load to actionbar.
And when user clicks the back button from fragment,fragment menu should remove then main activity menu should load to action bar.
Now this code is not removing the old menu,it's adding new menu to near of old menu.
How can I do this ?
You Can use menu.clear(); method.
It remove all existing items from the menu, leaving it empty as if it had just been created.
Try this..
#Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
menu.clear(); // clears all menu items..
getActivity().getMenuInflater().inflate(R.menu.fragment_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
You Should call setHasOptionsMenu(true); method in onCreateView.
It tells fragment got a menu.
Then Try below codes...
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_manage, container, false);
}
#Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
menu.clear(); // clears all menu items..
inflater.inflate(R.menu.fragment_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Try this:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_menu, menu);
super.onCreateOptionsMenu(menu,inflater);
}
Hope it helps

menu inflating calls multiple times at fragment's onCreateOptionsMenu

I use Fragments and when I switch to nested Fragment, which implements public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) my menu inflates quantity of times when I get to that nested Fragment. How can I avoid this? I also implement constructor of Fragment with methods:
setRetainInstance(true);
setHasOptionsMenu(true);
When I tried to implement siple solution as:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
if(!isInflated)
{
inflater.inflate(R.menu.contacts_archive_menu, menu);
isInflated = true;
}
super.onCreateOptionsMenu(menu, inflater);
}
but my menu wasn't inflate after the screen rotation.
I solved it simply by clearing menu before ionflating of it:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.call_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Just check the count of menu items. Meaning menu.size()==0 ,no menu items are present,then inflate with layout menu,else don't inflate at all.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (menu.size() == 0)
inflater.inflate(R.menu.call_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Just override the invalidateOptionsMenu, example:
private var isMenuCreated = false
...
override fun invalidateOptionsMenu() {
Log.d("invalidateOptionsMenu")
if (!isMenuCreated) {
super.invalidateOptionsMenu()
}
}
...
override fun onCreateOptionsMenu(menu: Menu): Boolean {
...
isMenuCreated = true
return true
}
Use before replace.
fragment = new EditMyProfile();
FragmentTransaction fragmentTransactionEditProfile =getSupportFragmentManager().beginTransaction();
getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentTransactionEditProfile.replace(R.id.frame, fragment);
fragmentTransactionEditProfile.commit();

Error in onCreateOptionsMenu at detail activity

I have a listview and call detail activity when the item is selected.
My onCreateOptionsMenu has error on displaying the menu at Action Bar.
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(getActivity()).inflate(R.menu.detail_view_menu, menu);
return (super.onCreateOptionsMenu(menu));
}
The error is The method onCreateOptionsMenu(Menu, MenuInflater) in the type Fragment is not applicable for the arguments (Menu). Error happened at return line.
I implement listview and detail activity using fragmentTransaction.
Thanks
Your onCreateOptionsMenu(Menu menu) just needs to be in the activity that hosts the fragment, not in the fragment itself.
You could also consider extending a BaseActivity and including it in there just once.
public class BaseActivity extends Activity {
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(getActivity()).inflate(R.menu.detail_view_menu, menu);
return (super.onCreateOptionsMenu(menu));
}
}
public class ListActivity extends BaseActivity {
// ...
}
public class DetailActivity extends BaseActivity {
// ...
}
Try it like this but put it in your main Activity class:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.detail_view_menu, menu);
return true;
}
OR if you want your Fragment to add items to the ActionBar you have to use a slightly different construct:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.detail_view_menu, menu);
}
You have an additional parameter that you have to add(MenuInflater). Also, in a Fragment the onCreateOptionsMenu doesn't return a boolean value.
Now that you have your inflater you need to call setHasOptionsMenu(true) in your Fragment's onCreate() method. Otherwise your items will not be displayed in the ActionBar.
Your Fragment code, handling the menu inflation, should now look like this:
public class DetailFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.detail_view_menu, menu);
}
}

Can I have different menu for each tab of TabHost

Is that possible to have different menus for each tab of TabHost?
Yes, you can in onCreateOptionsMenu depending on the tab inflate a different menu
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
int tab = getTabHost().getCurrentTab()
if (tab==1)
inflater.inflate(R.menu.main_menu, menu);
else
inflater.inflate(R.menu.other_menu, menu);
return true;
}
You need to provide different versions of menu.xml files within res/menu for this.
If you're using fragments you can put setHasOptionsMenu(true); inside onCreate method of your fragment and override onCreateOptionsMenu.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_xxx, menu);
}

Categories

Resources