What are Retained Fragments?
What are Retained Fragments?
Retained fragments are a way to preserve the state of a fragment across configuration changes, such as screen rotations, in Android. When a fragment is marked as retained, the Android system will not destroy it when the hosting activity is re-created due to a configuration change[1][2].
To make a fragment retained, you call setRetainInstance(true)
in the fragment's onCreate()
method[1][3]. This tells the Android system to keep the fragment alive and not destroy it, even if the hosting activity is destroyed and re-created[2].
Retained fragments are useful for preserving long-running operations or complex state that should not be lost when the activity is re-created[3]. For example, you might use a retained fragment to hold a reference to an AsyncTask
or a network connection that should continue running despite configuration changes[1].
It's important to note that while the fragment itself is retained, its view hierarchy is still destroyed a...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào