I use 3 FrameLayouts in my activity view xml into which I dynamically insert different fragments. Many of these fragments contribute ActionBar MenuItems. Now I have a situation where I hide a FrameLayout (set visibility to View.GONE) and therefore the Fragment in it becomes invisible. However it still contributes the menu item since the fragment does not to seem to be paused or anything so I cant seem to call a method that actively hides the action bar item.
As a solution I now just insert a fragment into the FrameLayout that has no menu item when I switch the FrameLayout to invisible. While that works it feels like a hack to me. What is the proper way to hide any action bar menu items? What states does the fragment go into if I just hide the layout it is in?
I am doing all this with the compatibility library r6 in case that matters.
You should be able to call invalidateOptionsMenu() in your FragmentActivity derived activity which will cause all of the fragments onCreateOptionsMenu() methods to be called again, and you can hide any menu items you don't want visible any more.
I had a similar problem - an Activity with two FrameLayouts:
on the horizontal screen rotation both were visible,
on the vertical screen rotation only one was visible.
Both contained Fragments with some menu items. After switching from the horizontal to the vertical view menu items from both Fragments were visible. I have resolved it by using a popBackStack() functionality in the onCreate() method like in the code below:
getSupportFragmentManager().popBackStack();
FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
tr.replace(getListContainer(), listFragment);
if (detailPanelVisible) {
Fragment detailFragment = createDetailFragment();
tr.replace(getDetailContainer(), detailFragment);
}
tr.addToBackStack(null);
tr.commit();
Maybe this can do the work for you.
Related
Currently I have a ViewPager with 4 Tabs, each one is a different Fragment. I'm now switching my app to Bottom Bar Navigation but there's a problem with keeping the Fragment state and re-using it later.
The Problem :
Initially Tab_1 is visible. If user selects Tab_2 and then re-selects Tab_1, then I can see that the Fragment for Tab_1 goes through onCreate() and re-downloads the data from the server. With ViewPager I was using the setOffScreenPagesLimit() is there something equivalent with bottom bar ?
I have an activity with a NavigationDrawer and a full screen fragment. Clicking on various items in the NavigationDrawer inflates a different fragment in the activity.
I would like to also swap in a different toolbar when launching a different fragment. The reason I want to do that instead of inflating a new menu is that the toolbar I want to swap in is a bit complicated and has things like an EditText in it.
Is there some way to do this either in the activity before inflating a fragment or maybe in the fragment?
Just include your Fragment specific toolbar in your Fragment's layout so that it's visible in your Fragment permanently.
Now in the onStart() method of your Fragment -
getActivity().getSupportActionBar().hide();
This will hide the activity's default toolbar so that it does not overlap with the Fragment's toolbar.
Then again in the onStop() method of your Fragment -
getActivity().getSupportActionBar().show();
So that the Activity's toolbar is visible again outside that Fragment.
each time you call a fragment use "invalidateOptionsMenu" and using fragment id you can set visibility of menuitems in toolbar... if that is what u need... hope it helps
Let me brief about my idea and what I'm aiming to achieve,
Idea is to have a swipe tab with three tabs in it and each tab screen being represented by a seperate Fragment class. I did this using the "it.neokree:MaterialTabs:0.11" library and everything is working well.
Then I created a floating action button (FAB) using the "com.oguzdev:CircularFloatingActionMenu:1.0.2" library.
Now what I'm trying to achieve is to fix this FAB to the fragment in the center tab and when I swipe the screen to go the previous or the next tab the FAB should slide out along with the tab fragment its in and should slide back in along with the center tab screen when I swipe back to it.
I have worked on it till now and now I'm able hide the view in other fragments and show it again in the center fragment by overriding the setUserVisibleHint() method in each fragment class like this.
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
View tempView = getActivity().findViewById(R.id.myFAB);
tempView.setVisibility(View.INVISIBLE);
}
}
But the problem is I want the FAB to slide out and in along with the center tab just like other views in the tab's fragment class does, but my FAB is simply vanishing and reappearring.
In a way I want the FAB to be exclusive to the center tab and should be fixed to it.
I also tried defining the FAB inside the center Fragment class but it doesn't seem to make any difference. Any Advice and suggestion will be very helpful. Thanks in advance
This library adds it's view to the 'android.R.id.content' ViewGroup. That's why it doesn't matter, where you define it.
Animating such view may be difficult, but should be possible. You have translate the FAB while swiping the pager manually.
By the way, Google recommends to hide the FAB, not swipe:
For tabbed screens, the floating action button should not exit the
screen in the same direction as the screen exits. Doing so creates
visual noise. It would also cause a nonfunctional floating action
button to appear on screen. Furthermore, it incorrectly implies that
the floating action button is at the same the z-level as the content,
rather than at the level of the primary UI elements at the root
level.
See: http://www.google.com/design/spec/components/buttons-floating-action-button.html#buttons-floating-action-button-behavior
If you want to show the FloatingActionButton only in the middle/single tab, then you can add the FAB in the Fragment instead of adding it in Activity in which all the Fragments are added.
Inside my app I am using navigation drawer for my navigation. For each "page" I am using fragments (like showed here: http://developer.android.com/training/implementing-navigation/nav-drawer.html). It works completely fine, but now I want to create swipe tabs. I want to display those tabs only on one of the fragments. Problem is, that tabs are displayed on all fragments, which is ofcourse perfectly normal because tabs are inside action bar. What should I do, to display tabs only on one fragment?
From what I have understood, you are interested in showing action bar only in a particular fragment (not visible when swiping in subsequent fragments). The best way would be to code the action bar in fragment instead of using the action bar in main fragment and then creating swipe views (or sub fragments).
Hope it helps.
I'm using the code from the Example section of this page http://developer.android.com/guide/components/fragments.html
In landscape mode, the activity displays two fragments side-by-side, and on rotation, it launches a new activity containing the "detail" fragment.
The problem is that if I add a menu item to the actionbar from the detail fragment then rotate the screen to portrait to launch the detail fragment in a new activity then exit the activity, the menu item is still displayed even though the fragment supplying the menu item has been removed.
I've tried removing the detail fragment with a FragmentTransaction in onResume and then calling invalidateOptionsMenu(), but it doesn't remove the menu item.
I'm using ActionBarSherlock, and I've also tried supportInvalidateOptionsMenu()
Has anyone else run into this problem?
I thought I was removing the fragment, but I wasn't. After removing the fragment in onResume, my problem was solved.