Android Navigation Component - Unwanted blinking on screen navigation - android

I'm using navigation component
'androidx.navigation:navigation-fragment-ktx:2.3.3'
'androidx.navigation:navigation-ui-ktx:2.3.3'
I don't understand why when I'm navigating from my startDestination to the next Fragment
I get a short blinking and I see the previous activity for a couple of millisec.
This doesn't happen when I'm navigating to different fragments.
This happens with and without the navigation animations.
Tried with no previous Activity in stack and it shows the OS home screen.
The navigation.xml file looks like this:
<fragment
android:id="#+id/barcodeScannerFragment"
android:name="com.sightplan.sightplanmobile.feature.barcode_scanner.BarcodeScannerFragment"
android:label="#string/receive_package_barcode_input_view_header_param_title"
tools:layout="#layout/barcode_scanner_fragment_layout">
<action
android:id="#+id/open_receive_package_details"
app:destination="#id/receivePackageDetailsFragment"
app:enterAnim="#anim/slide_left_anim"
app:exitAnim="#anim/wait_anim"
app:popEnterAnim="#anim/wait_anim"
app:popExitAnim="#anim/slide_right_anim" />
</fragment>
<fragment
android:id="#+id/receivePackageDetailsFragment"
android:name="com.sightplan.sightplanmobile.packages.receive_package.ReceivePackageDetailsFragment"
android:label="#string/receive_package_activity_on_create_header_bar_title"
tools:layout="#layout/receive_package_details_fragment_layout">
.......
The navigation (full) desgin looks like this:
And the code for navigating from the startDestination to the Receive Package screen is here:
val action = BarcodeScannerFragmentDirections.openReceivePackageDetails()
Navigation.findNavController(it).navigate(action)
Any ideas on how to solve this?

The problem was due to the theme set on my navigation Activity inside the AndroidManifest.xml.
I had a custom AppTheme with android:windowIsTranslucent set to true.
Removing that solved the issue.

Related

NavigationController does not open new fragment if it is already associated with bottom navigation

I have a fragment in bottom navigation when try to launch new fragment with same id using navigation controller instead of launching new fragment it redirects me to bottom navigation tab.
here is my code
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/home_navigation"
app:startDestination="#id/splashFragment">
<fragment
android:id="#+id/baseTabsFragment"
android:name="com.abc.abc.fragment.BaseTabsFragment"
android:label="BaseTabsFragment"
tools:layout="#layout/fragment_common_tabs">
<action
android:id="#+id/action_to_baseFragment"
app:destination="#id/baseTabsFragment"
app:launchSingleTop="false" />
</fragment>
</navigation>
And code to navigate.
findNavController().navigate(R.id.action_to_baseFragment)
Here is a link for more details on this
https://issuetracker.google.com/issues/262076827
Update: I want to avoid the code duplication. I will have same fragments with same actions but different id's looking for a better way to do it.
If i understand correctly, you want to navigate to the same fragment and fragment does not refresh.
If you use navigation component in visual interface you need to pull an arrow from your baseFragment onto baseFragment again, with this you will see an arrow like self pointed. If you don't use visual just paste below code and it will be created.
<action
android:id="#+id/action_to_BaseFragment"
app:destination="#id/baseTabsFragment"
app:popUpTo="#id/baseTabsFragment"
app:popUpToInclusive="true" />
We are using this way because if we want system to navigate, it needs different location then previous, as long as we using same fragment we need to pop the old one.
It happened due to bottom menu. In this case you need to call programmatically to click on that bottom button.
Replace
findNavController().navigate(R.id.action_to_baseFragment)
to
navController.navigate(
R.id.base_graph, null,
NavOptions.Builder().setPopUpTo(navController.graph.startDestinationId, true).build()
)

BottomNavigationView icons not highlighted with Navigation 2.4

I updated to Navigation 2.4 (def nav_version = "2.4") and now when tapping on a BottomNavigationView item it does not always highlight the icon or show the fragment the BottomNavigationView item id points to.
There are 3 bottom navigation tabs called Home, Actions, My Progress. From the Home fragment, you can navigate to SubFragment.
So the flow might be to start at Home --> go to SubFragment --> go to Actions with the BottomNavigationView --> and then tap on Home to go back. What this did before the update was open the Home fragment (desired behavior). Now when tapping on the Home icon it shows SubFragment and does not highlight the icon.
More details
This is the navController setup:
bottomNavigationView = findViewById(R.id.bottom_navigation_view)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
bottomNavigationView.setupWithNavController(navController)
The nav_graph structure is like this:
<fragment
android:id="#+id/home"
android:name="com.example.app.home"
android:label="Home"
tools:layout="#layout/home"
<action
android:id="#+id/action_home_fragment_to_sub_fragment"
app:destination="#id/sub_fragment"/>
</fragment>
<fragment
android:id="#+id/subfragment"
android:name="com.example.app.subfragment"
android:label="Sub Fragment"
tools:layout="#layout/subfragment" />
<fragment
android:id="#+id/actions"
android:name="com.example.app.actions"
android:label="Actions"
tools:layout="#layout/actions" />
<fragment
android:id="#+id/myprogress"
android:name="com.example.app.myprogress"
android:label="My Progress"
tools:layout="#layout/myprogress" />
The menu items id's for the BottomNavigationView are identical to nav_graph.
I thought the issue might be with the nav_graph structure not playing well with the new SDK, so I tried adjusting the structure of the nav_graph so that each navigation tab was within its own like this question answer has setup. It highlights the tab with this approach but still does not navigate to Home like the example above. Only to SubFragment.
Ideas on what I might be missing are appreciated!
Haven't figured out a fix for the issue yet, so downgraded to def nav_version = "2.3.5" and navigation works properly again.
This is the version before Navigation 2.4 as mentioned by # ianhanniballake above in comments.
I also came across with this issue. It is not a bug in the library.
Actually, when you link menu.xml with nav_graph.xml
You only specified only one fragmentId for each destination. Therefore, it is natural for icon not to change its current state when subfragment is selected.
Instead, you should use nested navigation graphs and use the id of that graph for menu.xml.
<navigation
android:id="#+id/home_destination"
app:startDestination="#id/home" >
<fragment
android:id="#+id/home"
android:name="com.example.app.home"
android:label="Home"
tools:layout="#layout/home"
<action
android:id="#+id/action_home_fragment_to_sub_fragment"
app:destination="#id/sub_fragment"/>
</fragment>
<fragment
android:id="#+id/subfragment"
android:name="com.example.app.subfragment"
android:label="Sub Fragment"
tools:layout="#layout/subfragment" />
</navigation>
<fragment
android:id="#+id/actions"
android:name="com.example.app.actions"
android:label="Actions"
tools:layout="#layout/actions" />
<fragment
android:id="#+id/myprogress"
android:name="com.example.app.myprogress"
android:label="My Progress"
tools:layout="#layout/myprogress" />
And when you are specifying the navigation_menu you will do like this:
<item
android:id="#+id/home_destination"
android:icon="#drawable/ic_read_quran"
android:title="#string/read_quran"
app:showAsAction="always" />

Add Transition File in Navigation Graph of Android Application

I want to add an explode transition in my next fragment using the latest Navigation pattern "Navigation Graph".How to add a transition XML file(#transition/fade or #transition/slide or #transiton/explode) into Navigation Graph of android app.
You will probably want to define the desired transition in your navigation graph like this:
<fragment
// here your fragment is defined
<action
android:id="#+id/your_action_id"
app:destination="#id/yourDestinationFragment"
app:enterAnim="#anim/fade_in"
app:exitAnim="#anim/fade_out"
app:popEnterAnim="#anim/fade_in"
app:popExitAnim="#anim/fade_out" />
</fragment>
Note that you can define different animations for entering and leaving a fragment.
The animations you define will go into the /res/anim folder. You might have to create it if you don't find it.

How to navigate from Dialog to Fragment in Navigation Component?

I am trying to navigation from DialogFragment to Fragment in Navigation Component, but getting weird result.
When I navigate from DialogFragment to Fragment, background fragment is changing to target fragment with current dialog on top of it, instead of just moving to target fragment.
Here is the navigation graph.
<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/home"
app:startDestination="#+id/titleScreen">
<fragment
android:id="#+id/titleScreen"
android:name="com.example.android.navigationadvancedsample.homescreen.Title"
android:label="#string/title_home"
tools:layout="#layout/fragment_title">
<action
android:id="#+id/action_title_to_about"
app:destination="#id/aboutScreen"/>
</fragment>
<dialog
android:id="#+id/aboutScreen"
android:name="com.example.android.navigationadvancedsample.homescreen.About"
android:label="#string/title_about"
tools:layout="#layout/fragment_about">
<action
android:id="#+id/action_aboutScreen_to_register"
app:destination="#id/register" />
</dialog>
<fragment
android:id="#+id/register"
android:name="com.example.android.navigationadvancedsample.formscreen.Register"
android:label="fragment_leaderboard"
tools:layout="#layout/fragment_leaderboard" />
</navigation>
Why I am getting this behavior or how to fix it?
By fixing I mean normal dialog behavior. Say, I have a dialog D on top of a fragment A and move to a fragment B from a button on D, the screen should show B. And when I pop back from B, it should go to previous stage of D on top of A.
Thanks #musooff for filing this bug
This problem was fixed on Navigation 2.1.0-alpha06, along with others dialog inconsistencies like back button when Dialog is popped.
However, update to 2.1.0-beta02 or higher if you can.
You could use
view.getDialog().dismiss();
after navigate to B. But in that way, dialog won't be visible when you came back to A fragment.
If you really want it to be visible, maybe you should try to use Fragment and pretend it to be Dialog. Like in these example with activity link.

Navigation component pop behavior is not working as it should [duplicate]

This question already has an answer here:
navigation component popUpTo bug
(1 answer)
Closed 2 years ago.
Inside an app I'm building I have used the single activity architecture and decided to use Google's new Navigation component to navigate around the app.
Though it's showing great promise, it has some drawbacks which my question is about one of them.
Assume that we have three fragments which are navigated in order, except that we want to go back to the first one when back button is clicked when we are on the third fragment. Here's how it goes:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_nav_graph.xml"
app:startDestination="#id/firstFragment">
<fragment
android:id="#+id/firstFragment"
android:name="com.hmomeni.navisample.FirstFragment"
android:label="fragment_first"
tools:layout="#layout/fragment_first" >
<action
android:id="#+id/action_firstFragment_to_secondFragment"
app:destination="#id/secondFragment" />
</fragment>
<fragment
android:id="#+id/secondFragment"
android:name="com.hmomeni.navisample.SecondFragment"
android:label="fragment_second"
tools:layout="#layout/fragment_second" >
<action
android:id="#+id/action_secondFragment_to_thirdFragment"
app:destination="#id/thirdFragment"
app:popUpTo="#+id/firstFragment" />
</fragment>
<fragment
android:id="#+id/thirdFragment"
android:name="com.hmomeni.navisample.ThirdFragment"
android:label="fragment_third"
tools:layout="#layout/fragment_third" />
</navigation>
The problem here is that when I want to repeat the navigation for a second time an exception occurs telling me that:
java.lang.IllegalArgumentException: navigation destination
com.hmomeni.navisample:id/action_firstFragment_to_secondFragment is
unknown to this NavController
Further investigation shows that upon hitting back button and returning to the first fragment, the navController.currentDestination still refers to the ThirdFragment which is wrong and it should be FirstFragment.
Any help on this is appreciated.
I was having an issue similar to this question but with circular navigation, where the back stack was not being popped. When navigating from C --> A, I was mistakenly setting the parameter for navigate(int resId) as
R.id.fragmentC
instead of using an action like
R.id.action_c_to_a
I found a somewhat workaround though I wouldn't call it a solution.
To hack around this issue one can create an action from ThirdFragment to itself and then check for navController.currentDestination when navigate is called.
I would not accept this answer for want of a real solution.

Categories

Resources