Previous fragment not loaded when pressing Back button using BottomNavigationView - android

I have an activity_main.xml with an BottomNavigationView and a NavHostFragment.
There are 2 tabs, HomeFragment with a video and FavoritesFragment. Within FavoritesFragment there is a list of favorite video's (thumbnails) and when I click on an item it loads the HomeFragment view with that specific video.
within mobile_navigation.xml I have defined the HomeFragment twice. 1 is the Home Tab and 1 is the "Favorite" when clicked on the thumbnail in FavoritesFragment.
When I click the back button I expect to be back in the FavoriteFragment. But the App shuts down.
When the thumbnail is clicked, this code is executed:
val action = FavoritesFragmentDirections
.actionNavigationFavoritesToNavigationFavorite(imageUri)
NavHostFragment.findNavController(this#FavoritesFragment)
.navigate(action)
I also tried this:
protected fun showFragment(fragment: Fragment, uniqueName: String) {
activity?.supportFragmentManager
?.beginTransaction()
?.replace(R.id.container, fragment)
?.addToBackStack(uniqueName)
?.commitAllowingStateLoss()
}
But same result.
With this:
override fun onBackPressed() {
super.onBackPressed()
if (!navController.popBackStack()) {
// Call finish() on your Activity
finish()
}
}
The back button goes back to the HomeFragment, instead of FavoritesFragment.
<?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/mobile_navigation"
app:startDestination="#+id/navigation_home">
<fragment
android:id="#+id/navigation_home"
android:name="eu.theappfactory.dailyrecipes.ui.home.HomeFragment"
android:label="#string/title_home"
tools:layout="#layout/fragment_home">
<argument
android:name="imageuri"
app:argType="string" />
</fragment>
<fragment
android:id="#+id/navigation_favorites"
android:name="eu.theappfactory.dailyrecipes.ui.favorites.FavoritesFragment"
android:label="#string/title_account"
tools:layout="#layout/fragment_favorites" >
<action
android:id="#+id/action_navigation_favorites_to_navigation_favorite"
app:destination="#id/navigation_favorite" />
</fragment>
<fragment
android:id="#+id/navigation_favorite"
android:name="eu.theappfactory.dailyrecipes.ui.home.HomeFragment"
android:label="#string/title_home"
tools:layout="#layout/fragment_home">
<argument
android:name="imageuri"
app:argType="string" />
</fragment>
</navigation>

Hey I don't know how kotlin works in android but here's how you should set it up,
onBack press only goes to the previous activity and if there isn't one it shuts down,
Since you have 1 activity and multiple fragments what you should do is override the onBackpress method and call the previous fragment and if none exist you should close the app, not after overriding onBackPress you will have to exit the app manually, I'm gonna share my java code, see if you can scavenge from it
private void onBackAction() {
getSupportFragmentManager().popBackStack();
}
Note the popBackStack only reopens a fragment if there is one the stack.

Try override onBackPressed method in the host activity and place the code:
override fun onBackPressed() {
if(!navController.popBackStack())
finish()
}

Related

how to remove startDestination when creating deepLink with NavDeepLinkBuilder?

imagine I have navGraph like this with startDestination="#id/rootFragment" :
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/nav_graph"
app:startDestination="#id/rootFragment">
<fragment
android:id="#+id/rootFragment"
android:name="com.nima.RootFragment"
android:label="rootFragment">
//some actions...
</fragment>
//other fragments...
</navigation>
and I want to create a Deeplink like this:
NavDeepLinkBuilder(context).
addDestination(R.id.FragmentA).
addDestination(R.id.FragmentB).
setGraph(R.navigation.nav_graph).
createPendingIntent()
so I want to show FragmentA first and FragmentB next, and when the user hits the back button only show the FragmentA and not showing the startDestination(rootFragment).
by default, the startDestination is in backStack, my question is how can I remove it?
NavDeepLinkBuilder has no method like PopUpto to remove it.

Cannot navigate to another Activity with Navigation Component

I'm trying to navigate to another activity (login) using Navigation Component.
Currently Activity_1 has navGraph1 and Activity_2 has navGraph2.
I've put a destination in navGraph1 for Activity_2. When I navigate, I get a blank screen, despite Activity_2 having a nav graph with a starting destination for another fragment.
onCreate is not being called, or is at least not being hit by my breakpoint in Activity_2.
When I nest navGraph2 in navGraph1, I can navigate to the starting destination fragment in navGraph2. This starting destination in navGraph2 is in compose. Activity_1 seems to bypass Activity_2 and hosts the fragment directly. However, I'd like Activity_2 to host it since navigating to Activity_2 can display more like a pop up modal, hiding things like the bottom nav bar.
I'm not sure what's going on here.
<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/nav_graph"
app:startDestination="#id/startFragment1">
<activity
android:id="#+id/Activity2"
android:name="com.example.Activity2"
android:label="Activity2" />
</navigation>
// in Activity 1
navController.navigate(R.id.activity2)
Activity 2
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/nav_graph_2"
app:startDestination="#id/startFragment2">
<fragment
android:id="#+id/startFragment2"
android:name="com.example.StartFragment2"
android:label="StartFragment2" />
</navigation>
class Activity2 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
setContentView(R.layout.activity2)
}
}
// Layout activity 2
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph_2" />
</androidx.constraintlayout.widget.ConstraintLayout>
Found the solution. Activity 2 was overriding the wrong onCreate(). It should be the onCreate() with only the savedInstanceState parameter.

Adding fragment manually into the backstack in Navigation Architecture Component

I am having 4 fragments in Navigation Graph, Named A, B, C, D, On which i am getting some data from the user.
When i an in a normal flow (A->B->C->D) and press back button everything(back-stack) working fine.
But i have a case in which i have and edit information flow, Suppose user left the form on Fragment D so when user come again on the screen, i have to navigate the user on the same screen i.e. Fragment D.
I achieved it via Deep-link.
Now the problem is in the above case there will be no fragment in the Stack other than fragment D. But i need when user press back button it should navigate to Fragment C to Fragment B to Fragment A .
Means i want other Fragments(A,B,C) to add in the back-stack. Can we do the same, if yes, how?
===Here is the code===
my_navigation_graph.xml.
<fragment
android:id="#+id/aFragment"
android:name="in.demo.project.AFragment"
android:label="AFragment"
tools:layout="#layout/fragment_a_layout">
<deepLink
android:id="#+id/deepLink"
android:autoVerify="true"
app:uri="demo://editflow?is_for_edit={is_for_edit}&step={step}" />
<argument
android:name="is_for_edit"
android:defaultValue="false"
app:argType="boolean" />
<argument
android:name="step"
android:defaultValue="2"
app:argType="string" />
<action
android:id="#+id/action_aFragment_to_bFragment"
app:destination="#id/bFragment"/>
<action
android:id="#+id/action_aFragment_to_cFragment"
app:destination="#id/cFragment"/>
<action
android:id="#+id/action_aFragment_to_dFragment"
app:destination="#id/dFragment"/>
<fragment
android:id="#+id/bFragment"
android:name="in.demo.project.BFragment"
android:label="BFragment"
tools:layout="#layout/fragment_b_layout">
<action
android:id="#+id/action_bFragment_to_cFragment"
app:destination="#id/cFragment" />
</fragment>
<fragment
android:id="#+id/cFragment"
android:name="in.demo.project.CFragment"
android:label="CFragment"
tools:layout="#layout/fragment_c_layout">
<action
android:id="#+id/action_cFragment_to_dFragment"
app:destination="#id/dFragment" />
</fragment>
<fragment
android:id="#+id/dFragment"
android:name="in.demo.project.DFragment"
android:label="DFragment"
tools:layout="#layout/fragment_d_layout">
</fragment>
Navigating through.
findNavController().navigate(AFragmentDirections.actionAFragmentToBFragment())
Inside AFragment i am redirecting to other fragment, assuming that
creating deep-link to every fragment is not good practice.
This code is in onCreate() Method of AFragment
arguments?.let {
if (AFragmentArgs.fromBundle(it).isForEdit) {
redirectionForEdit(AFragmentArgs.fromBundle(it).step)
}
}
and a function for redirection
private fun redirectionForEdit(step: String) {
when (step) {
"2" ->
findNavController().navigate(AFragmentDirections.actionAFragmentToBFragment())
"3" ->
findNavController().navigate(AFragmentDirections.actionAFragmentToCFragment())
"4" ->
findNavController().navigate(AFragmentDirections.actionAFragmentToDFragment())
}
}
Now if i get 4 in the step it'll redirect to DFragment, so the stack would be A->D, but i want B & D into the stack too (A->B->C->D).
For this please refer handling custom back navigation under navigation components
OnBackPressedCallback callback = new OnBackPressedCallback(true /* enabled by default */) {
#Override
public void handleOnBackPressed() {
// Handle the back button event
}
};
requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);

Android navigation component back Button doesn´t work [duplicate]

I use navigation components to navigate from one fragment to another. However, when the user press the back button, I want to navigate back to first fragment. But it keep showing the second fragment. This is my nav_graph:
<?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/nav_graph"
app:startDestination="#id/fragment1">
<fragment
android:id="#+id/fragment2"
android:name="com.myapp.ui.fragments.Fragment2"
android:label="fragment_2" />
<fragment
android:id="#+id/fragment1"
android:name="com.myapp.ui.fragments.Fragment1"
android:label="fragment_1">
<action
android:id="#+id/action_fragment1_to_fragment2"
app:destination="#id/fragment2"
app:enterAnim="#anim/fragment_fade_enter"
app:exitAnim="#anim/fragment_fade_exit"
app:popUpTo="#id/fragment1" />
</fragment>
</navigation>
And this is how I trigger the navigation in the code of my Fragment1-Class:
viewModel.itemSelected.observe(viewLifecycleOwner) {
navigate(it)
}
....
fun navigate(id: Long){
val bundle = Bundle()
bundle.putLong("itemid", id)
getNavController().navigate(R.id.action_fragment1_to_fragment2, bundle)
}
Edit:
Corrected startDestination in XML.
Edit2:
Added more code.
You're using a LiveData for an event. LiveData always caches the set value, so when you return to your Fragment1, you observe the LiveData again and get the same value a second time, causing you to navigate() yet again.
See this blog post for more information and alternatives.

Navigate up by back button with Navigation Component

I use navigation components to navigate from one fragment to another. However, when the user press the back button, I want to navigate back to first fragment. But it keep showing the second fragment. This is my nav_graph:
<?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/nav_graph"
app:startDestination="#id/fragment1">
<fragment
android:id="#+id/fragment2"
android:name="com.myapp.ui.fragments.Fragment2"
android:label="fragment_2" />
<fragment
android:id="#+id/fragment1"
android:name="com.myapp.ui.fragments.Fragment1"
android:label="fragment_1">
<action
android:id="#+id/action_fragment1_to_fragment2"
app:destination="#id/fragment2"
app:enterAnim="#anim/fragment_fade_enter"
app:exitAnim="#anim/fragment_fade_exit"
app:popUpTo="#id/fragment1" />
</fragment>
</navigation>
And this is how I trigger the navigation in the code of my Fragment1-Class:
viewModel.itemSelected.observe(viewLifecycleOwner) {
navigate(it)
}
....
fun navigate(id: Long){
val bundle = Bundle()
bundle.putLong("itemid", id)
getNavController().navigate(R.id.action_fragment1_to_fragment2, bundle)
}
Edit:
Corrected startDestination in XML.
Edit2:
Added more code.
You're using a LiveData for an event. LiveData always caches the set value, so when you return to your Fragment1, you observe the LiveData again and get the same value a second time, causing you to navigate() yet again.
See this blog post for more information and alternatives.

Categories

Resources