Switch tab on button click with Bottom Navigation and Navigation component - android

I have a very simple app that consists of three Fragments and a Bottom Navigation bar, created by using "New Project -> Bottom Navigation Activity" in Android Studio. The first Fragment holds a button, which should take me to the second Fragment, like if the middle button of the Bottom Navigation bar was clicked.
Is there a "standard" way to do this?
I have tried:
using launch(...) of the Navigation component, which seems to launch the Fragment with its own back stack and breaks the bottom navigation.
using setSelectedItemId(...) in different ways, which either results in an exception or breaks bottom navigation in different ways.
In this post, someone asks the exact same question, but it is marked as a duplicate. I fail to find the answer, especially concerning Navigation component.

Clicking the Button should have the same effect as if the user taps the corresponding item in the bottom navigation. So you need to call setSelectedItemId() on the BottomNavigationView. This can only be done in the Activity displaying the BottomNavigationView.
One option is to introduce a shared ViewModel with
a LiveData to be observed by the Activity
a function onButtonClicked() to be called by the OnClickListener of your Button which will update the LiveData
Once the LiveData observer fires, your Activity can call
binding.navView.selectedItemId = R.id.navigation_dashboard
Please note that for passing information about events like this one should choose some data type which can be invalidated after use. See for example LiveData with SnackBar, Navigation and other events (the SingleLiveEvent case)

Paste this code from where you want to go to the second fragment
Fragment fragment = new DashboardFragment();
FragmentManager fm = getActivity().getSupportFragmentManager();
fm.beginTransaction().replace(R.id.frame_layout, fragment).commit();
For more information click here

Related

How to prevent fragment detached in bottom navigation bar just like the NavigationAdvancedSample?

I have two question from the NavigationAdvancedSample, which is the sample android repository.
1
I found out that title fragment isn't detached while user moved from title fragment to the other fragment such as leaderboad fragment(or register fragment). But I don't know which code from the repository prevent detachment of the title fragment. You can see that the home fragment(which is same with title fragment) isn't detached when user come back to the title fragment.
But from my code, the fragment is detached when user came back to the fragment from the other fragment just like the below picture.
2
At the same repository, register fragment is detached when user move from register fragment to leaderboard fragment or home fragment. But I can't understand how the input data at the Edittext isn't removed. There aren't any viewmodel, but how can the text at the EditText isn't removed when user leave the fragment.
The problem was the version of the jetpack navigation.
Since I was using the navigation version as 2.3.5, the navigation was detaching.
The fragment was not detached from 2.4.0-alpha1 version. (I didn't check the other version.)

BottomNavigationView in child fragment in Navigation Component

I want to achieve something like this image flow in android navigation component. Where the Dashboard Fragment is the start destination. And from here i can navigate to another fragment which have a bottom navigation view. Is this possible using a single nav graph and a single activity? What is the best way to achieve something like this?
You can use BottomNavigationView
val childNavView: BottomNavigationView = binding.nav1View
val navController = NavHostFragment.findNavController(childFragmentManager.findFragmentById(R.id.navHostFragment) as NavHostFragment)
childNavView.setupWithNavController(navController)
In case someone's still looking for a solution.
Just use two separate navigation graphs. And when navigating, pick the right navigation controller object. You can use a single activity of course, with nested fragments. So your "Bottom nav page" will have its own navigation container with child fragments in it.
Moreover, you can also navigate from the inner nodes to outer ones. In that case you need to reference the parent fragment ("Bottom nav page") from the child fragment and then obtain its navigation controller like this:
parentFragment?.parentFragment?.findNavController()
Here the first parentFragment is NavHostFragment and its parent is your actual parent fragment ("Bottom nav page").
If you want Page 1-3 to have options for further fragments to navigate inside (i.e. Page 1.1, 1.2 for Page 1, etc), then you need a navgraph for each Page (so 3 total), I would also make them all the root of their own navigation, would make it a lot easier to implement, and just replace the dashboard fragment with an activity if it's for signing in for example.

show content of recyclcer view in fragment and bottomnavigation using navigation jetpack

I implemented the bottom navigation with Jetpack
in one tab i have recyclerview
I want to enter the details of an item when it is clicked and gone bottom navigation
What is the best way to do this?
using activity or fragment?
Fragments live within the same activity lifecycle, which means that switching from one tab to another won't destroy the activity data.
Fragments are therefore the best choice.
Here is a link to a step by step procedure on the implementation.
https://proandroiddev.com/step-by-step-to-bottom-navigation-with-jetpacks-navigation-component-and-multiple-nav-graphs-271c05af1dd3

Proper way to configure ActionBar when using Fragments

I have many fragments inside my activity. One fragment displays Top Books and when clicking on book this fragment is replaced by fragment showing details of clicked book.
Fragment showing top books should have no title in ActionBar, but drop down navigation with categories. However fragment with details of book should have title in ActionBar.
Here comes my question: how should I handle ActionBar changes in my application. Should I configure ActionBar inside Activity or inside Fragments? If in Activity how can I handle all those changes: back button pressing, up navigation button pressing?
Please help, this situation lacks of good examples.
I'd configure the ActionBar in the Activity.
You can override the onBackPressed() method to do different things depending on what fragment is showing.
This reference shows how to handle the up button being pressed, and you can change the logic depending on what fragment is showing as well: http://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp

Android Navigation Drawer with many fragments

I've been struggling with this for a while now, I want to start off with a diagram of my problem:
The three navigation drawer buttons are part of my Base Activity. Each purple block below the three buttons are fragments, and the descendants of each of those blocks are in turn fragments. I'll use the master and detail fragments as a demonstration of the issue I am having...The user clicks the nav drawer button, which opens the master fragment that hosts a list of articles. Once a user clicks one of those articles, I then open the detail fragment inside of the master fragment. So If I find myself at the detail, and I decide to open the nav drawer and click the third button for instance, then click the second button again, I want the detail to be open still, and if I hit the phone's back button I want it to move back to the master fragment, and end there. Any tips will be helpful, as I am probably going to use a similar pattern for the first button, it's main fragment and it's children fragments as well.
In my opinion, the cleanest way to handle what you are describing would be to have three separate FragmentActivity classes that implement the DrawerLayout instead of one monolithic BaseActivity.
Each button in the drawer should start it's respective FragmentActivity using launchMode singleTask. This ensures that you launch the same activity instance each time, instead of a new one, which will maintain your back stack for each activity as you switch between them using the drawer buttons. See Android Developer Guide Activity:launchMode for more details.
Each of the three FragmentActivity instances should be responsible for starting and managing it's fragments using listener interfaces. For example, where you have your Master fragment opening a Detail fragment directly, you should instead have your Master fragment tell it's FragmentActivity that it needs to open the Detail fragment. See Android Developer Guide: Communicating with Other Fragments for recommended practices in implementing this type of decoupled communication between FragmentActivity and Fragment. It will make your life much easier down the road when you want different layouts for tablets, etc.
Each of the three main drawer "tasks" seem to be unique enough that isolating each within it's own FragmentActivity seems the best way to implement what you are trying to do. You can apply this same approach for each of your main sections.
I had the same problem and didn't want to go to multiple activities since that would complicate the back navigation of my application. Your fragments won't save their state automatically unless the activity lifecycle events are being called.
In our case those don't happen since we're not leaving the activity. You can use FragmentManager's saveFragmentState on the fragment you are replacing to manually trigger the state saving and get a Fragment.SavedState object. You can keep a list of your SavedState objects and when pushing a fragment check if you have a saved state for it. If so you can call Fragment setInitialSavedState which will cause your fragment to load the previous state.
Now in my app as a user toggles between fragments with their own child fragments the state is retained when they come back.

Categories

Resources