https://kotlinlang.org logo
#android
Title
# android
j

Justin Xu

03/14/2023, 2:26 AM
How do I exit `androidx.navigation`'s navGraph-scoped viewmodel scope so that those viewmodels can be destroyed, and then reconstructed when I re-enter the navGraph's scope? Right now, I am calling
(context as Activity).finish()
inside my fragment, but that doesn't seem to be right.
i

Ian Lake

03/14/2023, 2:38 AM
That automatically happens when you pop the last destination that is the part of that graph off the back stack
c

chanjungskim

03/14/2023, 3:51 AM
If your project is SAA, why not?
j

Justin Xu

03/14/2023, 4:39 AM
I can't seem to
popBackStack
into the calling activity(that is not part of the nav_graph). This is how I start the activity that inflates the navGraph
Copy code
startActivity(Intent(applicationContext, MainActivity::class.java))
finish()
i

Ian Lake

03/14/2023, 4:41 AM
That seems like an entirely different question than your original question?
j

Justin Xu

03/14/2023, 4:42 AM
Well yes, this is my result after trying your solution
I'm not sure if I'm inflating/exiting the navgraph correctly now
i

Ian Lake

03/14/2023, 4:43 AM
Sounds like a bit of an XY Problem kind of situation: https://xyproblem.info/
It isn't clear how 'when I re-enter the navGraph's scope' was tied to you having multiple activities
But yes, the documentation does specifically call out when you manually pop the last destination on your back stack by returning false - that's when you need to manually finish your activity: https://developer.android.com/guide/navigation/navigation-navigate#back-stack
But if you're in those kind of cases, you're probably a bit far off the beaten path
20 Views