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.
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
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.
Let's say I have a FragmentA visible.
I add a FragmentB (keyword: add, not replace), add it to the Fragment back stack and commit.
The issues I'm having doing this is:
1) The action buttons of the action menu of FragmentB are added, but those of FragmentA are not removed.
2) The title of the ActionBar does not change (despite calling getActivity().setTitle("FragmentB") in onResume() of FragmentB.
I can resolve both of these by calling replace instead of add when showing FragmentB however, for quite a few reasons I specifically need to add the Fragment instead (one of them being, I need to retain the state of FragmentA while showing B).
So how would I go about updating the ActionBar correctly as described?
Try this piece of code:
getActivity().getActionBar().setTitle("FragmentB");
Use this code in your Activity.. (for setting title to your fragment).
public void setActionBarTitle(String title) {
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(title);
}
from your fragment onResume(), call this using
// Set title bar
((MyActivity) getActivity())
.setActionBarTitle("Fragment A");
and in each fragment you need to override onCreateOptionsMenu() to load your menus of that fragment.
I have a navigation drawer project with some fragments, now I want to call an activity from the menu.
The fragments are loaded into a framelayout and replaced with a new fragment on click event menu with a transaction.
When I click on the menu item I call this code:
fragment = new HomeFragment();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
Now if I call an activity with this code:
Intent intent = new Intent(MainActivity.this, asd.class);
startActivity(intent);
the activity override the entire navigation drawer because I don't replace the existing fragment into frame_container with the new activity.
How can I do this? Thanks!
Fragment can be added to the container. Activity hosts a fragment. What is happening is you are Navigating to a different Activity. You have a drawer in MainActivity not asd.
If you want a drawer in asd also create a BaseActivtiy that has NavigationDrawer and extend from it.
Your Navigation Drawer and the FrameLayout, all are inside one Activity where in you can replace the fragments and at the same time drawer will be visible.
However, when you start a new Activity, a new screen appears which do not have the implementation of the Navigation Drawer as it belongs to your first Activity only.
Use #Raghunandan's way and create a new BaseActivity which is extended by both of your Activites (one containing FrameLayout and the other one) or in case it is possible to use another Fragment instead of an Activity then create a new fragment then you can simply replace the new one in your FrameLayout and you will have the NavigationDrawer as well. :)
I've got an activity with an actionbar, and actionbar tabs. When I choose an item from the contents of the tabs, I'm trying to replace the current fragment with a new one, add the transaction to the back stack, and hide the tabs.
I hide the tabs by changing the action bar navigation mode to standard.
The problem is that when I press the back button, I just get a blank view with the action bar (in standard mode).. The fragment transaction doesn't appear to be reversed.
If I don't hide the tabs by changing the navigation mode to standard, the transaction reversal works fine..
I've tried overriding the back press to change the navigation mode back to tabs, but it doesn't work.
Could someone tell me how they would achieve this?
Here is the code where the tabs get hidden and the fragment transaction takes place:
ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Fragment albumListFragment = new AlbumListFragment();
albumListFragment.setArguments(bundle);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.content, albumListFragment);
ft.addToBackStack(null);
// Commit the transaction
ft.commit();
For clarity: I would expect that pressing back after this transaction has been committed, the tabs would come back into view with the previous fragment. At the moment after pressing back it's not showing the tabs OR the fragment.
Not sure if this question is still relevant or not, but I seem to be having good luck hiding the tabs after the fragment transition.
From my ListFragment, I do a standard fragment transition very similar to how you're doing it in your code.
In my detail fragment, however, I hide the action bar in the onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
}
and show it again in my onPause method:
public void onPause() {
super.onPause();
ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}