Child Fragment Not able to inflate Menu - android

Hi i am trying to inflate or change menu from child fragment , but not able to do so.
The flow is like this .
If I try to inflate menu from activity (Main)--- work fine.
if i try to infalte menu from fragment (SecondFrag)----work fine
if i try to inflate menu from child fragment of(SecondFrag)----do not work
Sample Code is like this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
Log.d("It has option mener of not", ""+hasOptionsMenu());
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//super.onCreateOptionsMenu(menu, inflater);
Log.d("Option Menu", "Activity Option Menu");
mActivity.getSupportMenuInflater().inflate(R.menu.deal_detaisl, menu);
Log.d("Option Menu", "Activity Option Menu");
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.deal_loc:
mActivity.showMapFragment();
break;
case R.id.deal_loc_route:
mActivity.showMapRoute();
break;
}
return false;
}

#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.deal_detaisl, menu);
super.onCreateOptionsMenu(menu, inflater);
}
This should work for you, try it.
Edit:
You should create a custom Fragment and make the fragments extend that custom Fragment. Then in your Activity for onCreateOptionsMenu you can get the current Fragment and call a public method for inflating a menu (call it whatever). After a chat discussion, your problem was solved.

Related

Unable to receive click on menu items in Fragment

I'm having a ToolBar made as ActionBar in a Fragment. I'm able to add ActionBar menu items but I'm not able to receive click response when I click on any ActionBar menu item.
I have read a lot of similar questions, I tried all but I'm still facing issue, so asked a question here.
The Fragment
public class DetailFragment extends Fragment {
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
setHasOptionsMenu(true);
return inflater.inflate(R.layout.project_detail, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mToolBar = (Toolbar)view.findViewById(R.id.tb_toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(mToolBar);
mToolBar.setTitle(R.string.project_details);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.project_detail_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.options:
// do something
break;
}
return true;
}
In my case in code above, onOptionsItemSelected is not even getting called.
Any pointers why ?
The Activity where I'm inflating this Fragment -
public class TestJust extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_just);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, new ProjectDetailFragment());
ft.commit();
}
}
project_detail_menu.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/options"
android:title="options"
android:icon="#android:drawable/ic_menu_more"
app:showAsAction="always"/>
</menu>
Most probably your problem is that you're always returning true from the activity's onOptionsItemSelected method.
When you return true, you consume the click event inside the method in the activity, that's why the click event never reaches the onOptionsItemSelected method inside your fragment.
Try this
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear()
inflater.inflate(R.menu.project_detail_menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.options:
// do something
return true;
default:
return super.onOptionsItemSelected(item);
}
Solution:
You are calling setHasOptionsMenu(true); in onCreateView()
Please call that method in onCreate()
Like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Calling this method in onCreate() ensures that the menu must participate in Fragment.
Then,
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_menu_items, menu);
}
Finally,
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.options:
// do something
break;
}
return super.onOptionsItemSelected(item);
It should work now.
initialize option in onCreateOptionMenu then check it. you only inflate menu, use without initialize option, first initialize then use it.
like: MenuItem item = menu.findItem(R.id.option);
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
MenuItem item = menu.findItem(R.id.option);
}
switch (item.getItemId()) {
case R.id.options:
// do something
return true;//<- Here you need to return true because breaking compile from this line returns default false;
default:
return super.onOptionsItemSelected(item);
}

How to hide the option menu when using getSupportFragment

Good Morning Guys,
i am trying to hide the option menu on some fragment.For the example i just want the option menu show on Promotion page
i add the code in the promotion.java
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
inflater.inflate(R.menu.toolbar_menu, menu);
super.onCreateOptionsMenu(menu,inflater);
}
and then i want to hide on other fragment.
when i launching the app and the first fragment is like
but when i click to the promotion page and click back to the menu page and the action bar will be like this
i am using getSupportFragment to call the menu item, and this problem just only happen on the fragment using the getSupportFragment call out.
case R.id.menu:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MenuFragment()).addToBackStack(null).commit();
If you want to control the option menu from Fragment you must call setHasOptionsMenu(true) in onCreate() of the Fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Clear the menu fromonCreateOptionsMenu():
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
}
OR, you can show/hide specific menu item by overriding onPrepareOptionsMenu:
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem item = menu.findItem(R.id.your_menu_item);
item.setVisible(false);
}
In the fragment onCreate add setHasOptionsMenu(true)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
then
#Override
public void onPrepareOptionsMenu(Menu menu) {
MenuItem item=menu.findItem(R.id.action_search);//your id instead of action_search
item.setVisible(false);
}

How do you set a Menu item listener (onMenuItemSelected) in a FragmentPagerAdapter?

I'm trying to have a Fragment inside a FragmentPagerAdapter have its own onclicklistener for a MenuItem.
In other words, I'm trying to use an Activity's onOptionsItemSelected method inside a Fragment
How do I do this?
I've tried using onContextItemSelected(MenuItem theItem) and onOptionsItemSelected(MenuItem theItem) to no avail
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
…
setHasOptionsMenu(true);
…
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.your_menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.your_key_id:
saveRoute();
break;
default:
break;
}
return false;
}
Use this if you need one menu for all the pages.
Edit:
If you need a specific menu on a specific page, see this: Android: Showing Action Bar menu items depending on ViewPager

Trying to hide/disable entire menu (overflow) in fragment

I have been unsuccessful in trying to hide or disable the overflow menu in one fragment.
I have tried setting setHasOptionsMenu(false) with no success, and then I tried setHasOptionsMenu(true) and tried inflating with an empty menu like below.
Both attempts do not work for me.
How do I hide or disable the options/overflow menu in one fragment only??
Thanks in advance!
Fragment
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.empty, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Figured out how to do it. Just need to set the menu items I don't want visible. In my case, I set all of them not visible and that removed the overflow menu entirely.
Also don't forget you have to setHasOptionsMenu(true) in your onCreate so it knows to call onCreateOptionsMenu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (menu != null) {
menu.findItem(R.id.menu_settings).setVisible(false);
menu.findItem(R.id.menu_leave_feedback).setVisible(false);
menu.findItem(R.id.menu_shop).setVisible(false);
}
}
I know this answer is very late but I like to share it.
In Fragment onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
And in Fragment onCreateOptionsMenu() method:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
}
setHasOptionsMenu(true)
in OnCreate() and
#Override
public 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);
}

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);
}
}

Categories

Resources