I have a navigation fragment which accepts data to display from bundle arguments. After user action, this fragment is 3 level deeper from the nav host activity. There is option to go next or previous (multiple times) which should reload the same fragment, but with newer data. For next/prev loading, if I create bundle and call Navigation.findNavController(it).navigate
, on an action pointing to itself, it just keeps on adding same fragment to the stack.
inside <navigation>
tag of file app:navGraph="@navigation/mobile_navigation"
along with other fragments
<fragment
android:id="@+id/questionFragment"
android:name="activities.qacenter.QuestionFragment"
android:label="fragment_question_center"
tools:layout="@layout/fragment_question">
<argument
android:name="questionObj"
app:argType="model.Question" />
<action
android:id="@+id/action_questionFragment_self"
app:destination="@id/questionFragment" />
</fragment>
Click listener for forward button inside override fun onViewCreated(view: View, savedInstanceState: Bundle?)
forward.setOnClickListener {
if (thisQ?.questionNo != null) {
val curQNo = thisQ?.questionNo!!
val curLevelNo = thisQ?.qOfLevel!!
val totalQCount = thisQ?.totalQCount!!
if (curQNo < totalQCount) {
val bundle = Bundle()
val etActivity = context as eTLanding
val wholeQSet = etActivity.getSelectionSet()!!
var questionSet = wholeQSet[curLevelNo-1].questions!!
var nextQuestion = questionSet[curQNo]
bundle.putSerializable("questionObj", nextQuestion)
Navigation.findNavController(it).navigate(R.id.action_questionFragment_self, bundle)
} else {
forward.isEnabled = false
}
}
}
How can I replace the currently displayed fragment with popToLeft/Right animation for previous and next button clicks?
I am using onViewCreated
to bind data to view elements and attach button listeners
This worked with minor adjustments like adding more arguments as per my need
internal fun navigateWithAnimation(actionId: Int, beginStackFrag: Int, anyView: View, isBackNav: Boolean, bundle: Bundle) {
val navBuilder = NavOptions.Builder()
val navController = Navigation.findNavController(anyView)
// val currNavigationNode = navController.graph.findNode(beginStackFrag)
if (isBackNav) {
navBuilder.setEnterAnim(R.anim.slide_out_right)
} else {
navBuilder.setEnterAnim(R.anim.slide_out_left)
}
navBuilder.setPopUpTo(beginStackFrag, false)
navController.navigate(actionId, bundle, navBuilder.build())
}
Alternative How to add animation transition to NavigationUI in android?