In some project we use Navigation Architecture Component, in navigation graph we added Fragment with global action, but issue is when quickly click multiple times on button after which navigate to Fragment - Fragment opens multiple times.
I have some options to solve this issue:
class SafeClickListener( private var defaultIntervalMs: Long = 1000, private val onSafeCLick: (View) -> Unit ) : View.OnClickListener { private var lastTimeClicked: Long = 0 override fun onClick(v: View) { if (SystemClock.elapsedRealtime() - lastTimeClicked < defaultIntervalMs) { return } lastTimeClicked = SystemClock.elapsedRealtime() onSafeCLick(v) } }
use
app:launchSingleTop="true"
on global action, but still we see how multiple Fragments opens (this solution only solved popBackStack problem)Use custom fragment navigator where we e.g add custom attribute 'mustBeOnce' to Destination and in overrided method
navigate()
we will check if this destination already in our backstack.