ncapdevi / FragNav

An Android library for managing multiple stacks of fragments
1.5k stars 220 forks source link

Question - How to remove/destroy a root fragment #199

Closed KhoiHuynh closed 5 years ago

KhoiHuynh commented 5 years ago

how do i destroy/remove a root fragment? I want to do this because in one of my root fragments, a user will click a button, and i want that fragment to reset it's state.

I tried fragmentManager.beginTransaction().remove(AddLocationFragment()).commit() but no luck, any help would be appreciated. Thanks

nikhil-thakkar commented 5 years ago

Why would you want to destroy the fragment just to reset its state. I would say have reset method and reset the states of all the contributing variables. For e.g. If you want to make a editext to reset its text then just call setText with empty string and likewise. I hope this makes sense in your scenario.

KhoiHuynh commented 5 years ago

hmm, thank you. that actually sounds better. i'll give that a try

ncapdevi commented 5 years ago

Yup, going to agree with @nikhil-thakkar on this one. Removing a fragment can be an expensive operation and is entirely unnecessary if your only goal is to reset the state. I think you'd do yourself a great service to read some write-ups on managing fragment/activity states. https://developer.android.com/guide/components/fragments This would be a good place to get you started.

That all being said, if you REALLY wanted to do it the way you were saying (which I don't recommend) you could use the replace method. Also, you should not ever be using the fragmentManager while also using FragNav. The documentation makes this clear.