Skip to content

Домашнее задание фрагменты#62

Open
uatilman wants to merge 10 commits intoAndroid-Developer-Basic:masterfrom
uatilman:master
Open

Домашнее задание фрагменты#62
uatilman wants to merge 10 commits intoAndroid-Developer-Basic:masterfrom
uatilman:master

Conversation

@uatilman
Copy link

Домашнее задание реализовано полностью

}
with(binding) {
fragmentAAButton.setOnClickListener {
parentFragmentManager.beginTransaction()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Код работает, потому что view FragmentA уже в иерархии активити, и parentFragmentManager находит fragment_a_container. По заданию FragmentAA должен быть child фрагментом, поэтому используем childFragmentManager. Это создает правильную иерархию, корректный lifecycle и отдельный back stack для дочерних фрагментов, что важно при поворотах и навигации

ActivityA
|
FragmentA (supportFragmentManager)
|
FragmentAA (childFragmentManager FragmentA)
|
FragmentAB (childFragmentManager FragmentA)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Исправлено.

}
with(binding) {
fragmentABButton.setOnClickListener {
parentFragmentManager.beginTransaction()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Используем childFragmentManager

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А тогда FragmentAB будет же child от фрагмента FragmentAA, а по заданию он должен быть child от FragmentA?
Проверил по hash FragmentManager. childFragmentManager в FragmentA совпадает по hash c parentFragmentManager в FragmentAA.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uatilman Юрий, вы правы! АА у нас через чайлд, а для АВ уже перент. Затроили меня буквенные фрагменты.


val onBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
val backStackEntryCount = parentFragmentManager.backStackEntryCount

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше считать childFragmentManager.backStackEntryCount и не добавлять корневой FragmentA в backStack активити. Когда в стеке дочерних фрагментов остается только FragmentA (размер = 1), закрываем активити

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Исправлено.

private lateinit var fragmentBA: FragmentBA
private lateinit var fragmentBB: FragmentBB

private val isTablet by lazy { resources.getBoolean(R.bool.is_tablet) }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

По заданию разделение должно быть по ориентации экрана (landscape/portrait), а не по размеру устройства. Проверяем resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Исправлено.


parentFragmentManager.beginTransaction()
.replace(R.id.fragment_b_a_container, fragmentBB)
.commit()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Когда FragmentBB отправляет цвет обратно, FragmentBA может быть не готов его принять. Также пользователь не может вернуться к FragmentBA. Добавьте транзакцию в backStack через addToBackStack(null), чтобы FragmentBA сохранялся и мог получить результат.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Исправлено.

binding.root.setBackgroundColor(color)
}

binding.fragmentBBButton?.setOnClickListener {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В портретной разметке FragmentBB отсутствует кнопка, хотя код ожидает ее. Из-за этого пользователь не может отправить цвет в FragmentBA

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Исправлено.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

setFragmentResultListener(COLOR_RESULT_KEY_BA_TO_BB) { _, bundle ->

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

По заданию FragmentBB должен только отправлять цвет в FragmentBA, а не получать. Немного запутались с реализацией :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Исправлено.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants