My question is how can I prevent that my programmatically created toolbar menuItem (Menu with submenus) gets overridden with the xml layout every time the fragment resumes.
I want to create my expensive toolbar menuitem only when fragment is first time created and not when it is resumes.
I inflat my toolbar in the onCreateOptionsMenu() and store an instance of the menu item.
private MenuItem menuItem;
private SubMenu expessiveSubmenu;
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_layout, menu);
menuItem= menu.findItem(R.id.menuItem);
}
The menuItem gets populated when the async loader finishs.
#Override
public void onLoadFinished(Loader<> loader, Data data) {
//Expensive call
expensiveSubmenu= makeExpensiveSubMenu(menuItem, data);
}
Now the menu is fully populated and visible in the toolbar and I also have an instance of my submenu.
Because onCreateOptionsMenu() is called evertime the fragment is resumed my menu get's overridden with the xml layout and I have to create the expensive submenu again. A mehtod like menu.addSubmenu(Menu) would solve my needs but I couldn't find one. Any ideas would be appreciated.
Related
I displayed a fragment A that implements a ViewPager with several fragments (nested fragments).
In my nested fragments, I inflate a menu with the following method.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.my_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
This question was already asked here. And i tried all the answers its not working.
My issue is
Everything working fine.but when i open another fragment(it have not any option menu) and get back to previous view pager fragment while clicking menu item onOptionsItemSelected not firing. When i swipe fragment in viewpager and come back to the previous one, when i click menu item its firing.
Its because viewpager maintain 3 fragment alive at a time. so when you come back, it set menu visibility status true to last fragment. thats why your menu item click not firing.
Use the following in the fragment where you keeping a viewpager in your case fragment A.
private boolean isInitial=true;
#Override
public void onResume() {
super.onResume();
if (!isInitial) {
int pos = viewpager.getCurrentItem();
if (pageAdapter.getItem(pos).getUserVisibleHint() && pageAdapter.getItem(pos).isVisible()) {
pageAdapter.getItem(pos).setMenuVisibility(true);
}
} else {
isInitial = false;
}
}
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.
I use a FragmentStatePagerAdapter to display fragments inside a ViewPager.
These fragments display 2 additional icons on top of the ones coming from the parent Activity.
These 2 icons are showed/hidden according to fragment displayed, so I tried to keep the Menu inflated in memory in order to modify it dynamically.
On my ViewPager, 3 fragments are always loaded, only the middle one is displayed.
The problem is the following: it seems that onCreateOptionsMenu and onPrepareOptionsMenu are called only for the first Fragment created, because of this, I get null pointers when I try to get the menu on the other fragments. Please see the code below:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//Get the 2 icons for the rates
inflater.inflate(R.menu.rate_fragment, menu);
//Get activity's icons
super.onCreateOptionsMenu(menu, inflater);
mMenu = menu;
}
Here are the functions I use to hide/show the menu items:
private void hideOption(int id){
MenuItem item = mMenu.findItem(id);
if(item != null){
item.setVisible(false);
}
}
private void showOption(int id){
MenuItem item = mMenu.findItem(id);
if(item != null){
item.setVisible(true);
}
}
But these functions fail as soon as the system tries to load the second Fragment.
My guess is that the menu is only created once for the FragmentStatePagerAdapter, but then how can I modify dynamically the menu? Do I have to modify it using the Activity?
Thanks!
I am trying to show/hide items in my action bar depending on which fragment is visible.
In my MainActivity I have the following
/* Called whenever invalidateOptionsMenu() is called */
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(this.myFragment.isVisible()){
menu.findItem(R.id.action_read).setVisible(true);
}else{
menu.findItem(R.id.action_read).setVisible(false);
}
return super.onPrepareOptionsMenu(menu);
}
This works great however, when the device is rotated there is a issue. After the rotation is complete onPrepareOptionsMenu is called again however this time this.myFragment.isVisible() returns false...and hence the menu item is hidden when clearly the fragment is visible (as far as whats shown on the screen).
Based on the Fragments API Guide, we can add items to the action bar on a per-Fragment basis with the following steps:
Create a res/menu/fooFragmentMenu.xml that contains menu items as you normally would for the standard menu.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/newAction"
android:orderInCategory="1"
android:showAsAction="always"
android:title="#string/newActionTitle"
android:icon="#drawable/newActionIcon"/>
</menu>
Toward the top of FooFragment's onCreate method, indicate that it has its own menu items to add.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
...
}
Override onCreateOptionsMenu, where you'll inflate the fragment's menu and attach it to your standard menu.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fooFragmentMenu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Override onOptionItemSelected in your fragment, which only gets called when this same method of the host Activity/FragmentActivity sees that it doesn't have a case for the selection.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.newAction:
...
break;
}
return super.onOptionsItemSelected(item);
}
Try using Fragment's setRetainInstance(true); when your fragment is attached to the Activity. That way your fragment will retain it's current values and call the lifecycle when device rotated.
Edit: This is a quick and dirty fix, see es0329's answer below for a better solution.
Try adding this attribute to your activity tag in your android manifest:
android:configChanges="orientation|screenSize"
I have a problem with FragmentStatePageAdapter (that I use as adapter for a ViewPager) and the menu items from the action bar.
When I launch the application, everything is fine. If I move the task to background (for example, pressing HOME button) and I start doing stuff until the activity is ended, then when I go back to my application (through the launcher or a notification I create) everything is fine except there are duplicated menu items in the action bar.
An important detail is that the only duplicated items are those that are created in the onCreateOptionsMenu() of each fragment I use in the ViewPager.
If I replaceFragmentStatePageAdapter with FragmentPageAdapter the items are not duplicated anymore, but the fragments are not shown in the ViewPager (getItem() function from the adapter is never called so it does not return any fragment).
Any ideas? A way to avoid FragmentStatePageAdapter to duplicate menu items? Maybe use FragmentPageAdapter but with a modification to show the fragments? A modification in my fragments?
Here are some code snippets from my application...
How menu items are created inside the fragments:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
/* Create play button */
final MenuItem mPlay = menu.add(R.string.play_all);
mPlay.setIcon(R.drawable.ic_play_all);
mPlay.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
mPlay.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
final List<Song> songs = getSongs();
if (songs.size() == 0) {
/* Show message */
Toast.makeText(mContext, R.string.no_song_list, Toast.LENGTH_LONG).show();
} else {
/* Play song list */
try { PlayManager.getService().playList(songs); } catch (Exception e) {}
}
return false;
}
});
/* Create menu */
super.onCreateOptionsMenu(menu, inflater);
}
How fragments are instantiated in the ViewPager adapter
#Override
public Fragment getItem(int position) {
final Class<?> cls = mTabs.get(position);
/* No tab */
if (cls == null)
return null;
/* Instantiate fragment */
final Fragment fragment = Fragment.instantiate(mContext, cls.getName(), null);
/* Add to list */
mFragments.put(position, fragment);
/* Return fragment */
return fragment;
}
Thanks!
PS: I tried to change the launchMode of the activity to "singleTop" and I also tried to return in getItem() the previosly created fragment (but that's useless as getItem() is never called when I return to the application, as I said before).
I found a solution to my problem a few days after posting my problem. I'm sorry for putting it here so late.
The following code fixed my problem:
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.remove("android:support:fragments");
}
As simple as writing that piece of code in the activity where I instantiate and use the fragments.
I hope this helps :)
Before inflating the options menu in your Fragment inside the ViewPager, check whether the Fragment instance is visible to prevent duplicated menu items:
// This should be in your Fragment implementation
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
if(isVisible())
{
inflater.inflate(R.menu.menu_fragment, menu);
}
super.onCreateOptionsMenu(menu, inflater);
}
I assume the problem is that the FragmentStatePagerAdapter creates a new Fragment each time you swipe. As a result, onCreateOptionsMenu() is called for each Fragment that is created.
There are, I think, two solutions:
Take the action bar manipulation out of the Fragments. Instead of setting an onclick for the menu item inside of the Fragment, you send a message to the current Fragment from the Activity.
Use Menu.findItem() to see if another Fragment has already added the MenuItem you want, and if so attach the current Fragment to it (in onPrepareOptionsMenu()).
I found some solution try it may be work
In your duplicate menu fragment
Add menu.clear() before inflate menu file in onCreateOptionsMenu(Menu menu, MenuInflater inflater)
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.main,menu);
}