Я разрабатываю приложение для Android с использованием Jetpack Compose и Fragments. У меня возникла проблема, из-за которой представление внутри фрагмента не отображается, когда приложение возвращается из фонового режима при определенных условиях.
Описание проблемы:
У меня есть составная функция SampleBanner, которая раздувает макет, содержащий FragmentContainerView. В этот контейнер я добавляю SampleFragment. Вот соответствующий код:
Код: Выделить всё
@Composable
fun SampleBanner(modifier: Modifier = Modifier, controller: SampleController) {
val context = LocalContext.current
val fragment = remember { mutableStateOf(null) }
AndroidViewBinding(modifier = modifier, factory = FragmentContainerLockBinding::inflate) {
fragment.value = fragmentContainerView.getFragment()
// Additional setup code
}
}
Код: Выделить всё
class SampleFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_sample, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Initialize views and load data
}
override fun onStart() {
super.onStart()
Log.d("FragmentLifecycle", "onStart called")
}
override fun onResume() {
super.onResume()
Log.d("FragmentLifecycle", "onResume called")
}
// Calling the following method resolved the issue
fun addView(v: View) {
fl_bottom.addView(v)
}
}
Когда приложение переходит в фоновый режим (например, экран выключается или приложение сворачивается) а затем возвращается на передний план, иногда представление фрагмента не отображается.
Что я пробовал:
Код: Выделить всё
• According to the report, the moment the issue was reproduced, I called the following method on the fragment and it started working:
fun addView(v: View) {
fl_bottom.addView(v)
}
Код: Выделить всё
1. Under what circumstances would a Fragment’s view not be visible when returning from the background?
2. How can I ensure that the Fragment’s view is properly recreated and displayed when returning from the background?
3. Is there a recommended way to manage Fragment lifecycle within a Jetpack Compose AndroidViewBinding to prevent this issue?
Код: Выделить всё
• The issue occurs on devices running Android 11 and above.
• The screen rotation is locked, so I cannot reproduce the issue using configuration changes.
• I tried simulating low memory conditions using ADB commands, but the problem persists.
• I’m not using setRetainInstance(true) in my Fragment.
• I couldn’t consistently reproduce the issue, so I wasn’t able to confirm the lifecycle methods that are called when the issue occurs.
Подробнее здесь: https://stackoverflow.com/questions/790 ... -compose-a