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);
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.
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/
I am using native Fragments and a fragment transaction with add() and hide(). When I go back to the original fragment with the back button, the ripple animation from android:background="?attr/selectableItemBackground" resumes from when I opened the fragment originally. How do I cancel an animation when hiding a fragment in a transaction?
getFragmentManager().beginTransaction()
.hide(this)
.add(R.id.fragment_container, fragment)
.addToBackStack(null)
.commit()
Edit:
I am changing my app to use ViewPager.
The App uses a Navigation Drawer and a Viewpager (with each tab contains a fragment).
From the actionbar I would like to show an extra full screen fragment instead of the other fragments. This fragment could contain e.g. the user settings.
And ...
It would be very nice to use the top-left "<" back button of the navigation drawer to leave the new fragment and show the viewpager with the tabs again.
The most beautiful option would be the new fragment pushing the other fragments aside.
I don't want to start a new Activity, as I am working with pleasure with Fragements in all my Apps.
The DialogFragment is not suitable (as I tried).
First thing you need to do is replace the fragment:
getFragmentManager()
.beginTransaction()
.replace(R.id.container, new Fragment())
.addToBackStack("TAG")
.commit();
You can use one of the various methods available to animate this, see http://developer.android.com/reference/android/app/FragmentTransaction.html
Disable the nav drawer and you should get a back button, if not you can call (I think you should have already done this to setup the nav drawer):
getActionBar().setDisplayHomeAsUpEnabled(true);
You'll need to handle the back button press by implementing onOptionsItemSelected:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// Go back
break;
}
}
You'll probably need to handle the hardware back button in your activity too.