Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect pop in Navigation Component

In Navigation Component, how can one detect if fragment is brought to front from a pop event?

I go from A to B, now I close B using back key, it returns to A, now in A (in onViewCreated event) I want to detect it's coming from B.

like image 788
AVEbrahimi Avatar asked Oct 14 '25 03:10

AVEbrahimi


1 Answers

If we're using a single NavController.

findNavController((R.id.nav_host_fragment)
    .addOnDestinationChangedListener{ hostController, destination, _ ->
        val push = currentBackStackSize < hostController.bacStack.size // else pop
        // Then save current backstack size
  }

This solution might not be always correct, but currently I can't think of an edge case. Please feel free to correct me.

like image 105
Vaughn Armada Avatar answered Oct 17 '25 20:10

Vaughn Armada



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!