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
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.9.3'
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
20 changes: 20 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,20 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity

class ActivityA : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_a)

if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.fragmentA, FragmentA())
.addToBackStack(null)
.commit()
}
}
}
31 changes: 31 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,31 @@
package otus.gpb.homework.fragments

import android.content.res.Configuration.ORIENTATION_PORTRAIT
import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity


class ActivityB : AppCompatActivity() {
var fragmentBA = FragmentBA()
var fragmentBB = FragmentBB()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_b)

val transaction =
supportFragmentManager.beginTransaction().replace(R.id.fragmentBA, fragmentBA)
.replace(R.id.fragmentBB, fragmentBB)
val orientation = resources.configuration.orientation

Log.d("TAG", orientation.toString())
Log.d("TAG", (orientation == ORIENTATION_PORTRAIT).toString())

if (orientation == ORIENTATION_PORTRAIT) {
transaction.hide(fragmentBB)
}
transaction.commit()
}
}
48 changes: 48 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,48 @@
package otus.gpb.homework.fragments

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.activity.OnBackPressedCallback

class FragmentA : Fragment() {

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
activity?.finish()
}
}
}
requireActivity().onBackPressedDispatcher.addCallback(this, callback)
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_a, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.buttonFragmentA).setOnClickListener {
val color = ColorGenerator.generateColor()
childFragmentManager.beginTransaction()
.replace(R.id.fragmentAA, FragmentAA.newInstance(color))
.addToBackStack(null)
.commit()
}
}
}
76 changes: 76 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,76 @@
package otus.gpb.homework.fragments

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.FrameLayout
import androidx.activity.OnBackPressedCallback

private const val ARG_COLOR = "color"
class FragmentAA : Fragment() {
private var color: Int? = null

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

val callback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (childFragmentManager.backStackEntryCount > 0) {
childFragmentManager.popBackStack()
}
else {
parentFragmentManager.popBackStack()
}
}
}
requireActivity().onBackPressedDispatcher.addCallback(this, callback)
}


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
color = it.getInt(ARG_COLOR)
}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_a_a, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val layout = view.findViewById<FrameLayout>(R.id.fragmentAALayout)
val value = arguments?.getInt(ARG_COLOR)
if (value != null) {
layout.setBackgroundColor(value)
}

view.findViewById<Button>(R.id.buttonFragmentAA).setOnClickListener {
val color = ColorGenerator.generateColor()
childFragmentManager.beginTransaction()
.replace(R.id.fragmentAB, FragmentAB.newInstance(color))
.addToBackStack(null)
.commit()
}
}


companion object {
@JvmStatic
fun newInstance(color: Int) =
FragmentAA().apply {
arguments = Bundle().apply {
putInt(ARG_COLOR, color)
}
}
}
}
53 changes: 53 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,53 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout

private const val ARG_COLOR = "color"
/**
* A simple [Fragment] subclass.
* Use the [FragmentAB.newInstance] factory method to
* create an instance of this fragment.
*/
class FragmentAB : Fragment() {
private var color: Int? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
color = it.getInt(ARG_COLOR)
}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_a_b, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val layout = view.findViewById<FrameLayout>(R.id.fragmentABLayout)
val value = arguments?.getInt(ARG_COLOR)
if (value != null) {
layout.setBackgroundColor(value)
}
}


companion object {
@JvmStatic
fun newInstance(color: Int) =
FragmentAB().apply {
arguments = Bundle().apply {
putInt(ARG_COLOR, color)
}
}
}
}
37 changes: 37 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,37 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.FrameLayout
class FragmentBA : Fragment() {

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_b_a, container, false)
}

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

view.findViewById<Button>(R.id.buttonFragmentBA)?.setOnClickListener {
val activityB = requireActivity() as ActivityB

parentFragmentManager.beginTransaction()
.hide(activityB.fragmentBA)
.show(activityB.fragmentBB)
.commit()
}

parentFragmentManager.setFragmentResultListener("result", this) { _, result ->
val value = result.getInt("color")
view.findViewById<FrameLayout>(R.id.fragmentBALayout).setBackgroundColor(value)
}
}
}
38 changes: 38 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,38 @@
package otus.gpb.homework.fragments

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

class FragmentBB : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_b_b, container, false)
}

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

view.findViewById<Button>(R.id.buttonFragmentBB).setOnClickListener {
val orientation = resources.configuration.orientation
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
val activityB = requireActivity() as ActivityB
parentFragmentManager.beginTransaction()
.hide(activityB.fragmentBB)
.show(activityB.fragmentBA)
.commit()
}

val result = Bundle()
result.putInt("color", ColorGenerator.generateColor())
parentFragmentManager.setFragmentResult("result", result)
}
}
}
10 changes: 10 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package otus.gpb.homework.fragments

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

findViewById<Button>(R.id.buttonA).setOnClickListener {
startActivity(Intent(this, ActivityA::class.java))
}

findViewById<Button>(R.id.buttonB).setOnClickListener {
startActivity(Intent(this, ActivityB::class.java))
}
}
}
16 changes: 16 additions & 0 deletions app/src/main/res/layout-port/activity_b.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentBA"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentBB"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Loading