BottomNavigationView with Fragment inside Fragment - android

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(); }

Related

How to keep data when navigate to another fragment use navigation direction?

I have 2 fragment: fragment1 and fragment2
I'm using Navigation to navigate fragment1 to fragment2:
val action = Fragment1Directions.actionFragment1ToFragment2()
navController.navigate(action)
My fragment1 contain RecyclerView with data change. When I clicked to item in fragment1's RecyclerView will open fragment2. And when I press back button I back to fragment1 with loss data.
I want to keep my data when move to fragment2 and back to fragment1 (fragment2 like popup in web). How can I do it.
Update:
I used View Model to keep LiveData. but when I back from fragment2 to fragment1, fragment1 reloaded. How can I open fragment2 and hold fragment1 (temp open fragment2). I'm using Navigation
Try using livedata to keep track of your data.
I found the solution:
use View Model to keep LiveData
use Dialog destinations for fragment2. Visit https://gist.github.com/utkukutlu/bd2e37253e7184179d923321e3ea72c1 for more detail with Dialog destinations
Learned experience:
Navigation fragment only replace not have add. I want Navigation act like add so I use Dialog destinations
I use Dialog destinations so I have to use View Model to keep LiveData when I update my data in fragment1

navigation menu item not highlighted on switch of fragment1 to fragment2 with button click in fragment1

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.

How to communicate between activity to fragment in navigation drawer?

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/

Proper top level navigations in android fragments like in Google Play

I have an Activity with navigation drawer and a default fragment set in to Activity when application starts.
I have 4 top level navigation in my navigation drawer
Fragment 1
Fragment 2
Fragment 3
Fragment 4
and switching the fragments inside the activity on click on each each navigation. I want implement the fragment navigation in such manner that from each top level navigation fragment, if user clicks back button it should first come to Main or default fragment and from there app should exit same like in Google Play. I call it master fragment.
eg:
Default(master) Fragment > Fragment 1
Fragment > Fragment 2
Fragment 2 -- Back pressed > Deafult fragment and like so.
What I have tried so far :
I have tried adding fragment in backstack but it doesn't help it takes me all fragment in stack.
getSupportFragmentManager().beginTransaction()
.add(R.id.container, selectedFragment)
.addToBackStack("naviagtion_stack")
.commit();
My each top fragments also have child fragments in stack so stack count also did not help me.
I don't want to remove and add my default fragment because as it fetches some data from network so recreation will make the network call again which i don't want .
I want exactly what Google Play does. I just want to know the logic.
Add your master fragment to backstack and remember the tag: fragmentManager.beginTransaction()
.add(R.id.main_layout, masterFragment)
.addToBackStack(INITIAL_STATE)
.commit();
Click on the navigation elements should do following before adding a corresponding fragment: fragmentManager.popBackStack(INITIAL_STATE, 0);
This call removes from backstack everything but your master fragment.
All fragment transactions (including navigation fragments) should generally do the same thing, for example:
fragmentManager.beginTransaction()
.add(R.id.main_layout, fragment)
.addToBackStack(null)
.commit();

Fragment visible on the background

I am working on an application that has three fragments which are defined in an XML file:
[HeaderFragment]
[MainFragment]
[FooterFragment]
The first screen initiates the three fragments, the Header- and FooterFragment are static so will not change content.
The MainFragment is initial a menu with buttons and a transparant background (MenuFragment). When I click an item in the menu I replace the MenuFragment with a new fragment (DetailsFragment) like this:
FragmentTransaction transaction = mFragmentManager.beginTransaction();
Fragment newFragment = new DetailFragment();
transaction.replace(R.id.content_container, newFragment);
transaction.addToBackStack(newFragment.getTag());
transaction.commit();
The DetailFragment shows up and when I press back, the MenuFragment appears and everything works how it should.
Here is my problem:
Inside my DetailFragment, I have a toggle option to filter the content, this is a button. When this is clicked, the DetailFragmentFiltered replaces the DetailFragment on the same way as the code above states. The only difference is that I don't add it to the BackStack because after filtering and pressing Back.. I still want to return to the MenuFragment.
When I clicked the filter button and press Back, the DetailFragment (or DetailFragmentFiltered) is shown behind my MenuFragment. Afcourse, I don't want this.
Make sure you don't use a static fragment relation to an XML by setting the first fragment as "android:name" in the layout.
Make with framelayouts the layout of the XML and add the fragments flexibly as shown in this tutorial:
http://developer.android.com/training/basics/fragments/fragment-ui.html

Categories

Resources