Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ run.sh
*.aab
output-metadata.json
/build.properties
*.hprof
*.hprof
*.log
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.activity:activity:1.8.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
android:supportsRtl="true"
android:theme="@style/Theme.Fragments"
tools:targetApi="31">
<activity
android:name=".ActivityB"
android:exported="false" />
<activity
android:name=".ActivityA"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
Expand Down
26 changes: 26 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/ActivityA.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class ActivityA : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_a)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
// Add FragmentA to this activity
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container_a, FragmentA())
.commit()
}
}
}
27 changes: 27 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/ActivityB.kt
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.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class ActivityB : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_b)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}

// Add FragmentA to this activity
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container_b, FragmentB())
.commit()
}
}
}
72 changes: 72 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/FragmentA.kt
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 android.widget.Button
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment

class FragmentA : Fragment() {
private var callback: OnBackPressedCallback? = null

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_a, container, false)
view.findViewById<Button>(R.id.button_open_fragment_aa).setOnClickListener {
openFragmentAA()
}
return view
}

private fun openFragmentAA() {
val randomColor = ColorGenerator.generateColor()

val fragmentAA = FragmentAA.newInstance(randomColor)
childFragmentManager.beginTransaction()
.replace(R.id.fragment_container_ax, fragmentAA) // Replace with container ID
.addToBackStack(null) // Optional: Add to back stack
.commit()
}

fun openFragmentAB() {
val randomColor = ColorGenerator.generateColor()

val fragmentAB = FragmentAB.newInstance(randomColor)
childFragmentManager.beginTransaction()
.replace(R.id.fragment_container_ax, fragmentAB) // Replace with container ID
.addToBackStack(null) // Optional: Add to back stack
.commit()
}

override fun onAttach(context: Context) {
super.onAttach(context)

callback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
// Реализуйте обработку нажатия на кнопку “Назад”, используя
// OnBackPressedDispatcher, таким образом, что по нажатию
// из стека фрагментов удаляется один фрагмент.
// После того как размер стека = 1, закрывайте активити.
if (childFragmentManager.backStackEntryCount > 1) {
childFragmentManager.popBackStack()
} else {
isEnabled = false
requireActivity().onBackPressedDispatcher.onBackPressed()
}
}
}
requireActivity().onBackPressedDispatcher.addCallback(callback!!)
}

override fun onDetach() {
super.onDetach()
callback?.remove() // Удаляем обработчик при отсоединении фрагмента
callback = null
}

}
44 changes: 44 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/FragmentAA.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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 android.widget.Button
import androidx.fragment.app.Fragment

class FragmentAA : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

val view = inflater.inflate(R.layout.fragment_aa, container, false)

val color = arguments?.getInt(ARG_COLOR) ?: Color.WHITE
view.setBackgroundColor(color)

view.findViewById<Button>(R.id.button_open_fragment_ab).setOnClickListener(
{
if (parentFragment is FragmentA)
(parentFragment as FragmentA).openFragmentAB()
}
)

return view
}

companion object {
private const val ARG_COLOR = "color"

fun newInstance(color: Int): FragmentAA {
val fragment = FragmentAA()
val args = Bundle().apply {
putInt(ARG_COLOR, color)
}
fragment.arguments = args
return fragment
}
}
}
36 changes: 36 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/FragmentAB.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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

class FragmentAB : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

val view = inflater.inflate(R.layout.fragment_ab, container, false)

val color = arguments?.getInt(ARG_COLOR) ?: Color.WHITE
view.setBackgroundColor(color)

return view
}

companion object {
private const val ARG_COLOR = "color"

fun newInstance(color: Int): FragmentAB {
val fragment = FragmentAB()
val args = Bundle().apply {
putInt(ARG_COLOR, color)
}
fragment.arguments = args
return fragment
}
}
}
40 changes: 40 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/FragmentB.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package otus.gpb.homework.fragments

import android.content.res.Configuration
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

class FragmentB : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_b, container, false)
val landscapeLayout = view.findViewById<View>(R.id.landscape_layout)
val fragmentContainer = view.findViewById<View>(R.id.fragment_container)
if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
// Ландшафтная ориентация: оба фрагмента на экране
landscapeLayout.visibility = View.VISIBLE
fragmentContainer.visibility = View.GONE

childFragmentManager.beginTransaction()
.replace(R.id.fragment_container_ba, FragmentBA())
.replace(R.id.fragment_container_bb, FragmentBB())
.commit()
}
else {
// Портретная ориентация: только FragmentBB
landscapeLayout.visibility = View.GONE
fragmentContainer.visibility = View.VISIBLE

childFragmentManager.beginTransaction()
.replace(R.id.fragment_container, FragmentBA())
.addToBackStack(null)
.commit()
}
return view
}
}
55 changes: 55 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/FragmentBA.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package otus.gpb.homework.fragments

import android.content.res.Configuration
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment

class FragmentBA : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

val view = inflater.inflate(R.layout.fragment_ba, container, false)

val color = arguments?.getInt(ARG_COLOR) ?: Color.WHITE
view.setBackgroundColor(color)

val openFragmentBB = view.findViewById<Button>(R.id.open_fragment_bb)

// Слушатель для получения цвета от FragmentBB
parentFragmentManager.setFragmentResultListener(COLOR_REQUEST, this) { _, bundle ->
val bgColor: Int = bundle.getInt(ARG_COLOR, Color.WHITE)
view.setBackgroundColor(bgColor) // Красим фон в полученный цвет
}

if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
openFragmentBB.visibility = View.GONE


} else {
openFragmentBB.apply {
visibility = View.VISIBLE
setOnClickListener {

parentFragmentManager.beginTransaction()
.replace(R.id.fragment_container
, FragmentBB.newInstance(ColorGenerator.generateColor()))
.addToBackStack(null) // Добавляем в стек, чтобы вернуться назад
.commit()
}
}
}
return view
}

companion object {
private const val ARG_COLOR = "color"
private const val COLOR_REQUEST = "colorRequest"
}
}
52 changes: 52 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/FragmentBB.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package otus.gpb.homework.fragments

import android.content.res.Configuration
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment

class FragmentBB : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

val view = inflater.inflate(R.layout.fragment_bb, container, false)

// Кнопка для генерации цвета и возврата результата в FragmentBA
view.findViewById<Button>(R.id.send_color).setOnClickListener {
val randomColor = ColorGenerator.generateColor()

if (resources.configuration.orientation != Configuration.ORIENTATION_LANDSCAPE) {
// Закрываем текущий фрагмент
parentFragmentManager.popBackStack()
}

// Передаем результат обратно в FragmentBA
val result = Bundle()
result.putInt(ARG_COLOR, randomColor)
parentFragmentManager.setFragmentResult(COLOR_REQUEST, result)
}

return view
}



companion object {
private const val ARG_COLOR = "color"
private const val COLOR_REQUEST = "colorRequest"

fun newInstance(color: Int): FragmentBB {
val fragment = FragmentBB()
val args = Bundle().apply {
putInt(ARG_COLOR, color)
}
fragment.arguments = args
return fragment
}
}
}
Loading