OnCreateOptionsMenu is not called during fragment transaction - android

I have a fragment that contains a search menu in the toolbar. when user first time click on navigation item, Fragment is added in activity which uses add method of FragmentManager and then it will be show, hide or remove from fragment container according to the logic.
My problem is when the first time I click on navigation item search menu is displayed in the toolbar but afterwards when I come back to this fragment, sometimes there will be blank toolbar without search menu. How can I solve this?
Here is the main part code of fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_search, container, false);
searchFragmentInstance = this;
filterManager = new FilterManager();
//set toolbar
Toolbar toolbar = view.findViewById(R.id.toolbar_search);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
//other code...
return view;
}
Display search menu in toolbar using this:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onPrepareOptionsMenu(menu);
inflater.inflate(R.menu.search, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getContext().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) item.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
//other code...
}

I solved this problem by using adding this code:
//this method is called when the hidden state of the fragment is changed
//so when fragment is beign displayed using show method of fragment transaction
//this will be called
#Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if(!hidden){
//when fragment is not hidden anymore
//convert the toolbar into actionbar
Toolbar toolbar=(Toolbar)(((AppCompatActivity)getActivity()).findViewById(R.id.toolbar_search));
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
}
}

Related

How to add Search View on Action bar on one of the fragments in tab layout dynamically?

Please provide me a better solution if you have I am working on android app in which on home Activity by using TabLaout&ViewPager i have added 5 tabs on below
Home
Social
Notice
Search
Links
I have set for MainActivity in values/style but i have placed custom action bar tag in search.xml. I know how to add SearchView on action bar in activity but i am facing issues using fragments. My goal is just to show search view widget on action bar when user will on "Search" tab. On other tabs i don't want to show. Further more I have added
<meta-data android:name="android.app.searchable" android:resource="#xml/searchable"/> in Manifest file in tag and setHasOptionsMenu(true);
in Search Fragment. But action bar is showing on search screen without search View Widget, please correct me where i am doing wrong.
You have to add the SearchView only in the searchFragment:
searchFragment class:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_news, container, false);
...
setHasOptionsMenu(true);
.....
return rootView ;}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_search, menu);
try {
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
// do your search
return false;
}
#Override
public boolean onQueryTextChange(String s) {
// do your search on change or save the last string or...
return false;
}
});
}catch(Exception e){e.printStackTrace();}
}
menu_search xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:icon="#drawable/action_search_btn"
android:title="Search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>

Hide MenuItem in one Fragment in FragmentActivity with swipe tabs functionality

I've a FragmentActivity with three swipe tabs. I've added a REFRESH Icon as MenuItem in actionbar and inflated that menu in FragmentActivity by overriding onCreateOptionsMenu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.live_stream_item, menu);
refreshMenuItem = (MenuItem) menu.getItem(0);
refreshMenuItem.setVisible(true);
return super.onCreateOptionsMenu(menu);
}
I've globally declared refreshMenuItem and wanted to set Visibility GONE refreshMenuItem.setVisible(false) in one fragment and make it visible in other fragments.
I've used setHasOptionsMenu in one fragment where I want refresh menu item Invisible.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
}
Even I tried to set that refreshMenuItem.setVisible(false) in setUserVisibleHint in that particular fragment to hide this menuItem.
Nothing works.....Please help me out.
set setHasOptionsMenu(true) in Fragment's onCreate method and
override the onPrepareOptionsMenu() in the fragment and set the visibility of the corresponding menu item.
#Override
void onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
menu.findItem(R.id.xxx).setVisible(false);
menu.findItem(R.id.yyy).setVisible(false);
}

Show navigation drawer menu item only when a Fragment is shown

I am trying to show a button in the actionbar when a Fragment is shown and to hide the button when the other Fragment are shown.
I Override the onCreateOptionsMenu method:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
MenuItem item= menu.findItem(R.id.action_example);
item.setVisible(true);
super.onCreateOptionsMenu(menu,inflater);
}
And use setHasOptionMenu(true):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
I have done a test and I noticed that initially the button doesn't appear in the other Fragment , but after I open the Fragment in which I put this code above, the button is shown also in the other Fragment.
What you are missing is removing the optionsMenu in the onDestroy of the fragment. The behaviour you describe is logical with your code: when the Fragment is created you also create the options menu. It will not automatically be destroyed when the Fragment is destroyed.

Inflate Android Menu

I have an activity with a ViewPager object (with two pages). The first page should not show any menu. But the second one should show a menu with one item.
The problem is, if I inflate the menu in the onCreateOptionsMenu with any menu.xml, everything work nice, I mean, the menu changes when I change the ViewPager's page ... but I don't want tho show any menu in the first page.
So, I thought not to inflate the menu in onCreateOptionsMenu(by this way, I will not show any menu in the first page, and I will inflate it when I show the second page), but if I don't inflate the menu in the onCreateOptionsMenu, the menu is not shown anymore!
Here is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item_list_activity);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
if (position == 1) {
//Show user's menu
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.configure_users_menu, mMenu);
} else if (position == 0) {
//Clear menu. Don't show nothing.
mMenu.clear();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//If I don't inflate the menu here, the menu does not inflate anymore.
//If I inflate the menu here (with any menu), the menu works nice.
/*MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.configure_groups_menu, menu);*/
mMenu = menu;
return true;
}
I've tried to use menu.clear() after inflate the menu in the onCreateOptionsMenu, but it does not work.
Thanks a lot!!
You should not be inflating menu's yourself, only do this in the onCreateOptionsMenu method.
This is wrong:
#Override
public void onPageSelected(int position) {
if (position == 1) {
//Show user's menu
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.configure_users_menu, mMenu);
} else if (position == 0) {
//Clear menu. Don't show nothing.
mMenu.clear();
}
}
I created a step by step guide on how to achieve the "per page menu". This method will automatically hide and show menu's per Fragment (page). (Assuming your mAppSectionsPagerAdapter contains Fragments)
Step 1:
Don't inflate any menu's in your Activity, but if you do those menu items will always be shown.
Step 2:
Call the setHasOptionsMenu method in the onCreate of the Fragment with the menu
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Step 3:
Inflate the menu for the page (Fragment), do this inside the fragment code! Because the setHasOptionsMenu has been set to true the menu will be shown, but only if the fragment is active in the pager.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//TODO Inflate menu here!
super.onCreateOptionsMenu(menu, inflater);
}
Full example code (Randomly googled): http://www.java2s.com/Code/Android/UI/Demonstrateshowfragmentscanparticipateintheoptionsmenu.htm

Retrieve action layout from ActionBar, inside fragment

How do I retrieve a action layout for an item inside the ActionBar, from
a Fragment. I have tried to access the layout directly via getActivity().findViewById,
and via getActivity().findViewById(MenuItem).findViewById(ActionLayout).
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
MenuItem commentMenuItem = (MenuItem) getActivity().findViewById(R.id.menu_item_comment);
View actionView = commentMenuItem.getActionView();
TextView commentTextView = (TextView)
actionView.findViewById(R.id.action_bar_comment_item_textview);
commentTextView.setText(article.getArticle_comment_count());
}
Call getActionView() on the MenuItem corresponding to that action layout. You get the MenuItem from findItem() on the Menu, which you get in onCreateOptionsMenu().

Categories

Resources