-
Notifications
You must be signed in to change notification settings - Fork 68
Домашнее задание фрагменты #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
54f4ca6
1f4b855
e8681c8
ce40163
1af4c28
bdcaadf
818fac3
8c593b6
b12663f
65861ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package otus.gpb.homework.fragments | ||
|
|
||
| import android.os.Bundle | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import otus.gpb.homework.fragments.databinding.ActivityABinding | ||
|
|
||
| class ActivityA : AppCompatActivity() { | ||
|
|
||
| private lateinit var binding: ActivityABinding | ||
|
|
||
| private lateinit var fragmentA: FragmentA | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| binding = ActivityABinding.inflate(layoutInflater) | ||
| setContentView(binding.root) | ||
| fragmentA = if (savedInstanceState == null) { | ||
| FragmentA.newInstance() | ||
| } else { | ||
| supportFragmentManager.findFragmentByTag("fragmentA") as FragmentA | ||
| } | ||
| supportFragmentManager.beginTransaction() | ||
| .replace(R.id.fragment_activity_a, fragmentA, "fragmentA") | ||
| .commit() | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package otus.gpb.homework.fragments | ||
|
|
||
| import android.R.attr.orientation | ||
| import android.content.res.Configuration | ||
| import android.os.Bundle | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import otus.gpb.homework.fragments.databinding.ActivityBBinding | ||
|
|
||
| class ActivityB : AppCompatActivity() { | ||
|
|
||
| private lateinit var binding: ActivityBBinding | ||
|
|
||
| private lateinit var fragmentBA: FragmentBA | ||
| private lateinit var fragmentBB: FragmentBB | ||
|
|
||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| binding = ActivityBBinding.inflate(layoutInflater) | ||
| setContentView(binding.root) | ||
|
|
||
| if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) { | ||
| fragmentBA = savedInstanceState | ||
| ?.let { supportFragmentManager.findFragmentByTag(FragmentBA::class.simpleName) as? FragmentBA } | ||
| ?: FragmentBA() | ||
|
|
||
| fragmentBB = savedInstanceState | ||
| ?.let { supportFragmentManager.findFragmentByTag(FragmentBB::class.simpleName) as? FragmentBB } | ||
| ?: FragmentBB() | ||
|
|
||
| supportFragmentManager.beginTransaction() | ||
| .replace(R.id.fragment_b_a_container, fragmentBA, FragmentBA::class.simpleName) | ||
| .replace(R.id.fragment_b_b_container, fragmentBB, FragmentBB::class.simpleName) | ||
| .addToBackStack(null) | ||
| .commit() | ||
| } else { | ||
| fragmentBA = savedInstanceState | ||
| ?.let { supportFragmentManager.findFragmentByTag(FragmentBA::class.simpleName) as? FragmentBA } | ||
| ?: FragmentBA() | ||
| supportFragmentManager.beginTransaction() | ||
| .replace(R.id.fragment_b_a_container, fragmentBA, FragmentBA::class.simpleName) | ||
| .addToBackStack(null) | ||
| .commit() | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package otus.gpb.homework.fragments | ||
|
|
||
| import android.content.Context | ||
| import android.os.Bundle | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import androidx.activity.OnBackPressedCallback | ||
| import androidx.fragment.app.Fragment | ||
| import otus.gpb.homework.fragments.databinding.FragmentABinding | ||
|
|
||
|
|
||
| class FragmentA : Fragment() { | ||
| private var _binding: FragmentABinding? = null | ||
|
|
||
| private val binding get() = _binding!! | ||
|
|
||
| private lateinit var fragmentAA: FragmentAA | ||
|
|
||
| override fun onAttach(context: Context) { | ||
| super.onAttach(context) | ||
|
|
||
| val callback = object : OnBackPressedCallback(true) { | ||
| override fun handleOnBackPressed() { | ||
| if (childFragmentManager.backStackEntryCount > 0) { | ||
| childFragmentManager.popBackStack() | ||
| } else { | ||
| isEnabled = false | ||
| requireActivity().onBackPressedDispatcher.onBackPressed() | ||
| } | ||
| } | ||
| } | ||
| requireActivity().onBackPressedDispatcher.addCallback(callback) | ||
| } | ||
|
|
||
| override fun onCreateView( | ||
| inflater: LayoutInflater, | ||
| container: ViewGroup?, | ||
| savedInstanceState: Bundle? | ||
| ): View { | ||
| _binding = FragmentABinding.inflate(inflater, container, false) | ||
| val root: View = binding.root | ||
| return root | ||
| } | ||
|
|
||
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
| super.onViewCreated(view, savedInstanceState) | ||
| fragmentAA = if (savedInstanceState == null) { | ||
| FragmentAA.newInstance(ColorGenerator.generateColor()) | ||
| } else { | ||
| childFragmentManager.findFragmentByTag("fragmentAA") as FragmentAA | ||
| } | ||
| with(binding) { | ||
| fragmentAAButton.setOnClickListener { | ||
| childFragmentManager.beginTransaction() | ||
| .replace(fragmentAContainer.id, fragmentAA, "fragmentAA") | ||
| .addToBackStack("fragmentA") | ||
| .commit() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun onDestroyView() { | ||
| super.onDestroyView() | ||
| _binding = null | ||
| } | ||
|
|
||
| companion object { | ||
| @JvmStatic | ||
| fun newInstance() = FragmentA() | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package otus.gpb.homework.fragments | ||
|
|
||
| import android.graphics.Color | ||
| import android.os.Bundle | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import androidx.fragment.app.Fragment | ||
| import otus.gpb.homework.fragments.databinding.FragmentAABinding | ||
|
|
||
| const val COLOR_ARG_PARAM = "color" | ||
|
|
||
| class FragmentAA : Fragment() { | ||
|
|
||
| private var _binding: FragmentAABinding? = null | ||
|
|
||
| private val binding get() = _binding!! | ||
|
|
||
| private lateinit var fragmentAB: FragmentAB | ||
|
|
||
| private var color: Int = Color.CYAN | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| arguments?.let { | ||
| color = it.getInt(COLOR_ARG_PARAM) | ||
| } | ||
| } | ||
|
|
||
| override fun onCreateView( | ||
| inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? | ||
| ): View { | ||
| _binding = FragmentAABinding.inflate(inflater, container, false) | ||
| val root: View = binding.root | ||
| binding.root.setBackgroundColor(color) | ||
| return root | ||
| } | ||
|
|
||
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
| super.onViewCreated(view, savedInstanceState) | ||
| fragmentAB = if (savedInstanceState == null) { | ||
| FragmentAB.newInstance(ColorGenerator.generateColor()) | ||
| } else { | ||
| parentFragmentManager.findFragmentByTag("fragmentAB") as FragmentAB | ||
| } | ||
| with(binding) { | ||
| fragmentABButton.setOnClickListener { | ||
| parentFragmentManager.beginTransaction() | ||
| .replace(fragmentAAContainer.id, fragmentAB, "fragmentAB") | ||
| .addToBackStack("fragmentA").commit() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun onDestroyView() { | ||
| super.onDestroyView() | ||
| _binding = null | ||
| } | ||
|
|
||
| companion object { | ||
| @JvmStatic | ||
| fun newInstance(param1: Int) = FragmentAA().apply { | ||
| arguments = Bundle().apply { | ||
| putInt(COLOR_ARG_PARAM, param1) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package otus.gpb.homework.fragments | ||
|
|
||
| import android.graphics.Color | ||
| import android.os.Bundle | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import androidx.fragment.app.Fragment | ||
| import otus.gpb.homework.fragments.databinding.FragmentABBinding | ||
|
|
||
| class FragmentAB : Fragment() { | ||
|
|
||
|
|
||
| private var _binding: FragmentABBinding? = null | ||
| private val binding get() = _binding!! | ||
|
|
||
| private var color: Int = Color.CYAN | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| arguments?.let { | ||
| color = it.getInt(COLOR_ARG_PARAM) | ||
| } | ||
| } | ||
|
|
||
| override fun onCreateView( | ||
| inflater: LayoutInflater, container: ViewGroup?, | ||
| savedInstanceState: Bundle? | ||
| ): View { | ||
| _binding = FragmentABBinding.inflate(inflater, container, false) | ||
| val root: View = binding.root | ||
| binding.root.setBackgroundColor(color) | ||
| return root | ||
| } | ||
|
|
||
| override fun onDestroyView() { | ||
| super.onDestroyView() | ||
| _binding = null | ||
| } | ||
|
|
||
| companion object { | ||
| @JvmStatic | ||
| fun newInstance(param1: Int) = | ||
| FragmentAB().apply { | ||
| arguments = Bundle().apply { | ||
| putInt(COLOR_ARG_PARAM, param1) | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package otus.gpb.homework.fragments | ||
|
|
||
| import android.os.Bundle | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import androidx.fragment.app.Fragment | ||
| import androidx.fragment.app.setFragmentResultListener | ||
| import otus.gpb.homework.fragments.databinding.FragmentBABinding | ||
| import otus.gpb.homework.fragments.utils.COLOR_RESULT_BUNDLE_KEY_BB_TO_BA | ||
| import otus.gpb.homework.fragments.utils.COLOR_RESULT_KEY_BB_TO_BA | ||
|
|
||
|
|
||
| class FragmentBA : Fragment() { | ||
| private var _binding: FragmentBABinding? = null | ||
| private val binding get() = _binding!! | ||
|
|
||
| private lateinit var fragmentBB: FragmentBB | ||
|
|
||
| override fun onCreateView( | ||
| inflater: LayoutInflater, | ||
| container: ViewGroup?, | ||
| savedInstanceState: Bundle? | ||
| ): View { | ||
| _binding = FragmentBABinding.inflate(inflater, container, false) | ||
| val root: View = binding.root | ||
| return root | ||
| } | ||
|
|
||
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
| super.onViewCreated(view, savedInstanceState) | ||
| fragmentBB = savedInstanceState | ||
| ?.let { | ||
| parentFragmentManager.getFragment(savedInstanceState, "fragmentBB") as? FragmentBB | ||
| } | ||
| ?: FragmentBB() | ||
|
|
||
| setFragmentResultListener(COLOR_RESULT_KEY_BB_TO_BA) { _, bundle -> | ||
| val color: Int = bundle.getInt(COLOR_RESULT_BUNDLE_KEY_BB_TO_BA) | ||
| binding.root.setBackgroundColor(color) | ||
| } | ||
|
|
||
| binding.fragmentBBButton?.setOnClickListener { | ||
| parentFragmentManager.beginTransaction() | ||
| .replace(R.id.fragment_b_a_container, fragmentBB, FragmentBB::class.simpleName) | ||
| .addToBackStack(null) | ||
| .commit() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Когда FragmentBB отправляет цвет обратно, FragmentBA может быть не готов его принять. Также пользователь не может вернуться к FragmentBA. Добавьте транзакцию в backStack через addToBackStack(null), чтобы FragmentBA сохранялся и мог получить результат.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Исправлено. |
||
| } | ||
|
|
||
| } | ||
|
|
||
| override fun onDestroyView() { | ||
| super.onDestroyView() | ||
| _binding = null | ||
| } | ||
|
|
||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Используем childFragmentManager
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@uatilman Юрий, вы правы! АА у нас через чайлд, а для АВ уже перент. Затроили меня буквенные фрагменты.