Firstly, when using Navigation, you must always use the NavController
APIs - specifically, use navController.popBackStack()
and not the FragmentManager
APIs directly.
Secondly, passing true
to popBackStack()
means an inclusive pop - i.e., also pop the destination you pass in. If you only want to pop A2 while you're on A2, you could
A) Use popBackStack()
(which pops just the topmost destination)
B) Use popBackStack()
with the ID of A2 and an inclusive of true
C) Use popBackStack()
with the ID of A1 and an inclusive of false
Thirdly, while calling popBackStack()
and then calling startActivity()
manually is totally fine, you can also use a combination of popUpTo
and activity destinations to pop your destination and navigate to an activity as a single operation.
<activity
android:id="@+id/activity_b"
android:name="com.example.ActivityB" />
Then add an action to navigate from A2 to B:
<action
android:id="@+id/a2_to_b"
app:popUpTo="@+id/inicio"
app:destination="@+id/activity_b" />
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…