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

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

class ActivityA : AppCompatActivity() {

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

supportFragmentManager.beginTransaction()
.replace(R.id.activity_a, FragmentA())
.commit()
}
}
16 changes: 16 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,16 @@
package otus.gpb.homework.fragments

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

class ActivityB : AppCompatActivity() {

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

supportFragmentManager.beginTransaction()
.replace(R.id.activity_b, FragmentB())
.commit()
}
}
54 changes: 54 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,54 @@
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 com.google.android.material.button.MaterialButton

class FragmentA : Fragment() {

private fun generateBackgroundColor() = ColorGenerator.generateColor()

override fun onAttach(context: Context) {
super.onAttach(context)
val callback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (childFragmentManager.backStackEntryCount > 1) childFragmentManager.popBackStack()
else {
isEnabled = false
requireActivity().onBackPressedDispatcher.onBackPressed()
}
}
}
requireActivity().onBackPressedDispatcher.addCallback(this, callback)
}

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

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<MaterialButton>(R.id.fragment_a_btn).setOnClickListener {
childFragmentManager.beginTransaction()
.replace(R.id.fragment_aa, FragmentAA.newInstance(generateBackgroundColor()))
.addToBackStack(null)
.commit()
}
}

fun createFragmentAB() {
childFragmentManager.beginTransaction()
.replace(R.id.fragment_aa, FragmentAB.newInstance(generateBackgroundColor()))
.addToBackStack(null)
.commit()
}
}
41 changes: 41 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,41 @@
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 com.google.android.material.button.MaterialButton

private const val COLOR_ARG = "color_arg"

class FragmentAA : Fragment() {

private fun getBackgroundColor() = arguments?.getInt(COLOR_ARG) ?: Color.WHITE

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_aa, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.setBackgroundColor(getBackgroundColor())
view.findViewById<MaterialButton>(R.id.fragment_ab_btn).setOnClickListener {
(parentFragment as FragmentA).createFragmentAB()
}
}

companion object {
fun newInstance(color: Int): FragmentAA {
fun setBundleArgument() = Bundle().apply { putInt(COLOR_ARG, color) }
return FragmentAA().apply {
arguments = setBundleArgument()
}
}
}
}
37 changes: 37 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,37 @@
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

private const val COLOR_ARG = "color_arg"

class FragmentAB : Fragment() {

private fun getBackgroundColor() = arguments?.getInt(COLOR_ARG) ?: Color.WHITE

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_ab, container, false)
}

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

companion object {
fun newInstance(color: Int): FragmentAB {
fun setBundleArgument () = Bundle().apply { putInt(COLOR_ARG, color) }
return FragmentAB().apply {
arguments = setBundleArgument()
}
}
}
}
31 changes: 31 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,31 @@
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? {
if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
childFragmentManager.beginTransaction()
.replace(R.id.fragment_ba, FragmentBA())
.replace(R.id.fragment_bb, FragmentBB())
.addToBackStack(null)
.commit()
return inflater.inflate(R.layout.activity_b_land, container, false)
} else {
childFragmentManager.beginTransaction()
.replace(R.id.activity_b, FragmentBA())
.addToBackStack(null)
.commit()
return inflater.inflate(R.layout.activity_b, container, false)
}
}
}
46 changes: 46 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,46 @@
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 androidx.fragment.app.Fragment
import com.google.android.material.button.MaterialButton

private const val COLOR_ARG = "color_arg"
private const val FRAGMENT_REQUESTED_COLOR = "fr_req_color"

class FragmentBA : Fragment() {

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_ba, container, false)
}

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

parentFragmentManager.setFragmentResultListener(FRAGMENT_REQUESTED_COLOR, this) { _, bundle ->
val backgroundColor = bundle.getInt(COLOR_ARG, Color.WHITE)
view.setBackgroundColor(backgroundColor)
}

val openBbBtn = view.findViewById<MaterialButton>(R.id.fragment_bb_btn)

if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
openBbBtn.visibility = View.GONE
} else {
openBbBtn.setOnClickListener {
parentFragmentManager.beginTransaction()
.replace(R.id.activity_b, FragmentBB())
.addToBackStack(null)
.commit()
}
}
}
}
36 changes: 36 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,36 @@
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
import com.google.android.material.button.MaterialButton

private const val COLOR_ARG = "color_arg"
private const val FRAGMENT_REQUESTED_COLOR = "fr_req_color"

class FragmentBB : Fragment() {

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_bb, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<MaterialButton>(R.id.send_color_btn).setOnClickListener {
val result = Bundle().apply { putInt(COLOR_ARG, ColorGenerator.generateColor()) }
parentFragmentManager.setFragmentResult(FRAGMENT_REQUESTED_COLOR, result)

if (resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
parentFragmentManager.popBackStack()
}
}

}
}
12 changes: 11 additions & 1 deletion 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 androidx.appcompat.app.AppCompatActivity
import com.google.android.material.button.MaterialButton

class MainActivity : AppCompatActivity() {

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

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

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

<FrameLayout
android:id="@+id/fragment_ba"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />

<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="@color/black" />

<FrameLayout
android:id="@+id/fragment_bb"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />

</LinearLayout>
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_a.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_a"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityA">
</FrameLayout>
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_b.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_b"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
Loading