I have a problem I am using the new navigation controller from Android Jetpack. I have 3 items in my bottom navigation menu, and the first item has 5 fragments one after another. When I click to the second item in bottom navigation, and return to my first item it opens the first fragment not the last opened. Because android:id="#+id/destination_home" in Menu is the same as the same as id in my navigation graph.
Can anyone advice the best solution? Thanks
For now navigation component not supports of saving "multi stack". This issue can be fixed with custom navigation controller: https://github.com/STAR-ZERO/navigation-keep-fragment-sample
P.S.: also in this thread guy has same problem and you can see the answer, maybe it will help you.
Related
I'm currently writing an app that displays a list of movies. I'm using a bottom navigation view with 3 tabs: Trending, Discover, and Favorites. Each of these display a list of movies and the user can press on the movie to go to the details fragment in each one, also I'm using one nav graph.
When I scroll a bit and go to the details and use the back button, the state of the recyclerview is saved. However if I go to another tab and come back the state is not saved. After extensive research I still can't figure out the answer.
Any help is appreciated.
I dare to assume that you do have not a dependency on the navigation fragment:
kotlin: androidx.navigation:navigation-fragment-ktx:$nav_version
java: androidx.navigation:navigation-fragment:$nav_version
Problem caused by Navigation Component. It use replace (Fragment) instead of add (Fragment).
You can remove Navigation Component and manage Fragment by yourself, use add instead of replace fragment.
Found the answer: The bottom navigation now automatically handles the save state, just make sure you have the latest versions of nav and fragment. You can find the latest nav version and fragment versions here:
https://developer.android.com/jetpack/androidx/releases/navigation
https://developer.android.com/jetpack/androidx/releases/fragment
Here is an article that explains everything you need to know
https://medium.com/androiddevelopers/navigation-multiple-back-stacks-6c67ba41952f
I am facing this issue of fragments recreation with Android navigation component. I have integrated bottom navigation and coupled it with the android navigation component. So, every time I click on a tab on bottom bar, the fragment is recreated and the old state is not persisted.
I want to retain the state of fragment even when I go to other tabs and come back. I am not finding any solution for it anywhere.
Except bottom navigation, I am using navController.navigate() method to navigate between different fragments.
Hi the problem is fixed in latest version 2.4.0-alpha01 now there is a support multiple backstack navigation
Checkout the link:
https://developer.android.com/jetpack/androidx/releases/navigation#version_240_2
There is a problem with Navigation to handle multiple back stacks. A similar solution to this problem is available here
You can also use ViewModels to preserve the view state.
As alternative you can use hide/show fragment instead of navigation:
Show hide fragment in android
I am using new Navigation component of Jetpack Library and it is pretty cool but i experience an annoying problem.
I use Navigation component with Bottom navigation view and i have three fragments: Home, Profile and Settings. Home fragment has RecyclerView and when user clicks an item from it, a new fragment appears. The problem is when i am in some state in Home fragment and go to for example profile fragment, this state is gone and new home fragment is created and i do not want this to happen.
How can i solve this problem?
Thanks in advance.
I think this issue exists in current navigation. In this year's dev summit they talked about fixing that issue.
I have a project with the following 4 layouts:
I have actually 1 activity holding a bottom app bar and the NavHostFragment where the fragments get injected. The Main Fragment is the home view. There is a Management and a Setting fragment, both are top-level views like the home view but doesn't depend on each other. These 3 fragments can be switched by clicking an item in the nav drawer. For simplification, I'm trying the new navigation architecture component.
Now I have some design questions:
Should I move the bottom app bar into the fragments, cause they don't depend on each other and the FAB button has another action, otherwise I had to change the onClickListener in the activity when the fragments switch?
1.1 Or should I even show the bottom app bar in the management fragment? Maybe just the top bar with the Up caret.
1.2 Or bottom app bar + top bar and Up caret
1.3 and what about the drawer icon, should I display it in the Mgmt fragment
Should I use a fragment or an activity for the Settings fragment? When I use a fragment, I have to move the bottom app bar into the fragments. Otherwise, the bottom app bar would be visible in the Settings Fragment
The Management Fragment has just a recycler view. Clicking on an item should open a DetailView. Should I use a fragment or an activity here?
I read the doc about the navigation architecture component and there is a section about customizing the destination. Also, ich checked the source code and know that the fragments get replaced.
Further, I checked out some frequently used Apps how they implemented the navigation with a nav drawer and noticed, they all replace their fragments. Why does no one hide/show the fragments, is there a reason not to doing this?
Assume we have a fragment with a listview that holds data collected from a database or another expensive task. So wouldn't it be better to show/ hide these fragments instead of replacing it?
Sorry, it's my first app and I'm really confused about this topic, and it seems there are no official recommendations about it out there not even Material Design guidelines don't really make a reference about it.
How would you do it?
setupWithNavController on a Toolbar (or subclasses like BottomAppBar) only set up the Up icon and the title - they do not hook up menu items added to the Toolbar.
As per the Tie destinations to menu items documentation, you must set up your own listener and call onNavDestinationSelected(). For a BottomAppBar, that would be done by setting a Toolbar.OnMenuItemClickListener:
val navController = Navigation.findNavController(this, R.id.navigation_fragment)
myBottomBar.replaceMenu(R.menu.menu_with_nav_item)
myBottomBar.setupWithNavController(navController)
// Connect MenuItems to the NavController
myBottomBar.setOnMenuItemClickListener { menuItem ->
menuItem.onNavDestinationSelected(navController)
}
I have a problem...I am developing an app in android studio.In the meantime,there is already a navigation drawer in it.But the problem is, it only appears in the home screen.When it goes to another screen, the navigation drawer is not shown. I am new to android studio but i know some of Java languages.Can anyone help me with providing anything that would help me make the navigation drawer show in all of slides and pages in my app.
from docs
Navigation Drawer
The navigation drawer is a panel that displays the app’s main
navigation options on the left edge of the screen. It is hidden most
of the time, but is revealed when the user swipes a finger from the
left edge of the screen or, while at the top level of the app, the
user touches the app icon in the action bar.
for achieve what you asked you have to use Fragments
Fragment represents a behavior or a portion of user interface in an
Activity. You can combine multiple fragments in a single activity to
build a multi-pane UI and reuse a fragment in multiple activities. You
can think of a fragment as a modular section of an activity, which has
its own lifecycle, receives its own input events, and which you can
add or remove while the activity is running (sort of like a "sub
activity" that you can reuse in different activities).
see this examples for more details
1. Navigation Drawer - android hive
2. Navigation Drawer exp 2
3. Navigation Drawer exp 3
Refer this answer also
Answer
You have to add fragments in that activity where the navigation drawer exist. Whenver the user will click on the option in the navigation drawer the view should be changed by replacing with the required fragment.So by using the fragments user will stay on the same activity but just the views will be changed in that activity. you can refer to the fragments documentation provided by android developers. https://developer.android.com/guide/components/fragments.html