In Navigation drawer ,activity1 and fragment1 both are present in MainActivity (Main activity of navigation drawer).
I want to know how to go from activity1 to fragment1 in this case?
If you are in activity 1 and want to load navigation drawer fragments then you can use back button to come back to MainActivity then load fragment from the navigation drawer. But if want to replace fragment from activity 1 then you can use this
Fragment var_fragment = new Fragment1();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, var_fragment);
transaction.addToBackStack(null);
transaction.commit();
but if you want to add fragment to the activity1 then use transaction.add(...);
Refer this tutorial to load fragment or activity in navigation drawer
http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
Related
I've got some problems understanding Fragments in Android.
The bottom navigation of my MainActivity has got three items: FragmentA, FragmentB, FragmentC.
FragmentC has got a button. When the user clicks that button, another FragmentD should display additional information.
FragmentD should have an up-button in the toolbar. When the user clicks on that button, he gets back to FragmentC.
When the user selects a different fragment from the navigation, FragmentD should disappear.
Inside the onClick of my button I've tried to use this
FragmentManager manager = requireActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.nav_host_fragment, new FragmentD());
transaction.addToBackStack(null);
transaction.commit();
with nav_host_fragment being the fragment container for the bottom navigation items of my MainActivity.
Why doesn't transaction.replace replace FragmentC with FragmentD? When I press the button, FragmentD appears above FragmentC and has got a transparent background. FragmentD also does not get replaced by the other fragments when I select a different item from the bottom navigation. Is it because I'm using the nav_host_fragment container for FragmentD?
How do I get the up-button?
The solution is to use Navigation instead of FragmentTransaction.
In the Navigation xml file I've added FragmentD and an action for FragmentC with FragmentD as destination. In the button's onClick I now call Navigation.findNavController(view).navigate(R.id.fragment_c_to_fragment_d);
To make the button work, I had to override onSupportNavigateUp:
#Override public boolean onSupportNavigateUp() {
return Navigation.findNavController(this, R.id.main_fragment).navigateUp() ||
super.onSupportNavigateUp(); }
I have two fragments in my app namely HomeFragment and FeedbackFragment. In addition to switching fragments on tab buttons, I would also need to switch between fragments on a button click that is within the fragment. The tab items are highlighted properly on switching fragments using tab clicks. But the tab items does not get highlighted when i switch to other fragment on a button click from fragment1
Below is the code used to switch across fragments on button click and it works.
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
But the respective tab icon of FeedbackFragment is not highlighted. Currently the navigation item of HomeFragment remains highlighted even after FeedbackFragment is replaced. How do I highlight the menu item of Feedback Fragment ?
I tried below approaches but nothing worked:
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
View view = bottomNavigationView.findViewById(R.id.tab_calls);
view.performClick();
Also,
MainActivity.mBottomBar.selectTabAtPosition(2);
Nothing worked. Please help.
Try using the method to select a tab as if it was tapped:
bottomNavigationView.setSelectedItemId(R.id.tab_calls)
What I have done now is in onResume of each fragment I try to de highlight all other tab bars and highlight the current tab bar again. I access the bottomNavigationMenu from each fragment and do this. Not sure if this is the right way to handle it but it works atleast.
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
So I have an activity with a fragment in it, the fragment has a navigation drawer and the page's content. When I open the drawer and click an item the fragment is replaced with a new fragment. When I press the back button I call popBackStack on the fragment manager and it goes back to the first fragment but the navigation drawer is open.
A couple things to note: When pressing an item in the drawer I call closeDrawers on the drawer layout and the drawer closes while the fragment is replaced. If I press the UP button in the action bar I can replace the fragment container with a NEW main fragment, but I'd prefer to be able to pop the fragment off the stack.
Why is the drawer sticking open when I return to the main fragment?
In the click listener for drawer items:
case 4:
drawerLayout.closeDrawers();
Fragment aboutFragment = new AboutFragment();
fragmentTransaction.replace(R.id.fragment_container, aboutFragment, "AboutFragment");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
break;
This is the UP arrow in the action bar, this works but is hacky:
case android.R.id.home:
getFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new MainActivityFragment())
.commit();
break;
replace
drawerLayout.closeDrawers();
with
drawerLayout.closeDrawer();
In the onCreateView of your firt fragment, just close the drawer
mDrawerLayout.closeDrawer(Gravity.LEFT);
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. :)