My application contains an activity with several fragments. The activity displays the ActionBar by default. However, there are several fragments, displayed in order, where the ActionBar should not show. For these fragments, I hide the ActionBar in the onCreate code with getActivity().getActionBar().hide(); . However, each time one of these fragments are loaded, the ActionBar flashes on the screen momentarily before disappearing.
How can I make the actionbar disappear before the fragments are displayed on the screen? For reference, below is the code I use for transactions between the fragments:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.commitAllowingStateLoss();
I would suggest two ways to solve this.
Reconsider your activity-fragment relation, should it be split into different activities, i.e. some activities with action bar, some without action bar.
Use toolbar, and remove all default actionbar, assign the toolbar to your fragments which need it instead of the activity. Be reminded that you are not using setSupportActionBar() as this is not part of the activity layout.
Related
I have 5 Fragments and of course my .MainActivity.
MainActivity
FragOne
FragTwo
---
FragFive
and then:
activity_main.xml
app_bar_main.xml
content_main.xml
nav_header_main.xml
lay1.xml
lay2.xml
---
lay5.xml
When the application is loaded it's a blank screen but then obviously when I click the navigation bar the pages will load.
My question is, is there anyway I can use a fragment as my launcher page, because if I tried making a home page on any other activity like content_main or acivity_main, obviously then it would show on every page which I don't want.
But then if I made a new activity and set that as the launcher,obviously that would work but then my navigation bar would be missing I presume? What's the best way to go about this?
What you want is not that big a problem. Just load a fragment using SupportFragmentManager in your MainActivity. If this Fragment is a one time view only (before you change to your other fragments using Navigation Drawer), then a simple loading of your launcher Fragment in the MainActivity is enough.
Example for loading it once in MainActivity:
In your onCreate method in MainActivity :
///////////Setting up Fragment as Starting Page////////////
LauncherFragment fragment = new LauncherFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment,"launcher");
fragmentTransaction.commit();
This Launcher Fragment won't be seen after you change to other Fragments using NavigationDrawer (unless you call it somewhere else, which according to your question you don't want)
If instead of using separate LauncherFragment you decide you want to load FragOne itself, you will need to manually highlight your Drawer's First item (FragOne) after setting up FragOne
navigationView.getMenu().getItem(0).setChecked(true); ////Set Navigation drawer manually
I have an application with a stack of Fragments added on top of each other.
The Fragments are animated to slide in when opened, and slide out when popped from the backstack. Here's a sample of where I open each Fragment:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_right,0,0,android.R.anim.slide_out_right);
ft.add(R.id.examplecontainer, examplefragment);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();
The animations work as expected, and everything is good.. except that my App also has a couple more Tabs managed by a FragmentTabHost.
When I switch to another Tab and come back, I want the top Fragment to appear without any animations. Instead, it slides in again.
Is there any way to disable animations when switching between tabs on FragmentTabHost? Or any way to remove/change the custom animations set to a fragment?
Thank you in advance for any hints/suggestions.
How can I completely remove the ActionBar from Certain Fragments. I want to remove it not just to hide it. I have Actionbar Tab Navigation. From that i added a new Fragmnt which didn't need actionBar. So i need to remove it. I can't hide it because when i pressed back button and moved to Tabs section, i need to show Action bar again.
This is how i am replacing Fragments.
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.frame_container, fragment)
.addToBackStack(null).commit();
The action bar is not part of your fragment. It's part of your activity.
You can create a new Activity with a theme Theme.Holo.Light.NoActionBar and open this Activity with :
getActivity().startActivity(new Intent(getActivity(), SecondActivity.class));
And inside your second activity you can display the fragment you want.
I recommend you this post to understand more the differences between Fragment and Activity: https://stackoverflow.com/a/10515807/3112836
If you're not using actionbar.hide() just because you have to show Action bar in other fragment, than I think you should give it a try by using :
actionbar.hide() inside onAttach method of your fragment and actionBar.show() inside your onDestroy() method.
I have an ActionBar with various navigation tabs on it. I am finding that in some circumstances (I do not understand them fully) fragment content is appearing on top of fragment content.
i.e. I visit one tab, then click a button that swaps the fragment for another, then click one of the other tabs, and the fragment content from the initial tab click is visible under the new fragment content. It seems I've built an app where it's possible to use the navigation to place fragment content on top of other fragment content, which is not what I want.
How can I ensure that when updating fragment content, the old content is removed correctly?
How can I ensure that when updating fragment content, the old content is removed correctly?
You are the one creating the FragmentTransaction that is being applied by the TabListener. In that FragmentTransaction, you are telling Android what fragments to add, what fragments to remove, etc. Make sure you are setting up the FragmentTransaction objects with the business rules you want.
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.