Issue
I want to go from FragmentA (RootFragment) to FragmentB but I do not want to recreate the view of FragmentA once it comes back from FragmentB.
I am using Jetpack Navigation for navigating between Fragments.
To achieve the above goal, I have a fragment Fragment like this:
class RootFragment : DaggerFragment() {
private var viewToRestore: View? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
return if (viewToRestore != null) {
viewToRestore!!
} else {
return inflater.inflate(R.layout.fragment_root, parent, false)
}
}
override fun onDestroyView() {
viewToRestore = this.view
super.onDestroyView()
}
override fun onDestroy() {
super.onDestroy()
}
}
But the FragmentA (RootFragment) is leaking once I reach Fragment B with the attribute viewToRestore.
Any solution that can work without leak but achieve the same goal?
Solution
The leak is a false positive. It is totally fine from a Fragment point of view to hold onto the View you create in onCreateView
and return it later, under the condition that your Fragment is not retained or otherwise kept longer than the Context used to create the view is alive.
Answered By - ianhanniballake
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.