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);
}
}
Related
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
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.
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();
I am creating am android application which has 4 different activities all having a common menu. To show menu in all activities generally I need to add this code in every files.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
Is there anyway that I can add this code in one single file & include to all activities?
Thanks in advance.
Create one Main Activity write your menu code in that activity and then extend other activities with Main Activity..
public MainActivity extends Activity
{
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return false;
}
}
And some TempActivity
public TempActivity extend MainActivity
{
/......
}
abstract class MenuHavingActivity extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
}
and then have your other activities extend MenuHavingActivity
edit: yeah, what user370305 said
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);
}