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
7 changes: 5 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ plugins {
}

android {
compileSdk 32
compileSdk 34

defaultConfig {
applicationId "otus.gpb.homework.fragments"
minSdk 23
targetSdk 32
targetSdk 34
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}

buildTypes {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
android:supportsRtl="true"
android:theme="@style/Theme.Fragments"
tools:targetApi="31">
<activity
android:name=".FragmentBActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
Expand Down
56 changes: 56 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,56 @@
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 lateinit var buttonOpenFragmentAA: Button

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)
buttonOpenFragmentAA = view.findViewById(R.id.buttonOpenFragmentAA)
buttonOpenFragmentAA.setOnClickListener {
childFragmentManager.beginTransaction()
.replace(R.id.container_aa, FragmentAA.newInstance(ColorGenerator.generateColor()))
.addToBackStack("fragmentA")
.commit()
buttonOpenFragmentAA.visibility = View.GONE
}
}

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

childFragmentManager.addOnBackStackChangedListener {
if (childFragmentManager.backStackEntryCount == 0) {
buttonOpenFragmentAA.visibility = View.VISIBLE
}
}
}
}
97 changes: 97 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,97 @@
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


private const val KEY_COLOR = "color"


/**
* A simple [Fragment] subclass.
* Use the [FragmentAA.newInstance] factory method to
* create an instance of this fragment.
*/
class FragmentAA : Fragment() {

private var color: Int? = null
private lateinit var buttonOpenFragmentAB: Button


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
color = it.getInt(KEY_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 colorHex = color
if (colorHex != null) {
view.setBackgroundColor(colorHex)
}

buttonOpenFragmentAB = view.findViewById(R.id.buttonOpenFragmentAB)
buttonOpenFragmentAB.setOnClickListener {
parentFragmentManager.beginTransaction()
.replace(R.id.container_aa, FragmentAA.newInstance(ColorGenerator.generateColor()))
.addToBackStack("fragmentA")
.commit()
buttonOpenFragmentAB.visibility = View.GONE
}

}

override fun onAttach(context: Context) {
super.onAttach(context)
val callback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (parentFragmentManager.backStackEntryCount > 0) {
parentFragmentManager.popBackStack()
} else {
isEnabled = false
parentFragmentManager.beginTransaction().remove(this@FragmentAA)
.commitAllowingStateLoss()
}
}
}
requireActivity().onBackPressedDispatcher.addCallback(this, callback)
}

companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param color Parameter 1.
* @return A new instance of fragment FragmentAA.
*/

@JvmStatic
fun newInstance(color: Int) =
FragmentAA().apply {
arguments = Bundle().apply {
putInt(KEY_COLOR, color)
}
}
}
}
39 changes: 39 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/FragmentBActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package otus.gpb.homework.fragments

import android.content.res.Configuration
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import otus.gpb.homework.fragments.ui.FragmentBA
import otus.gpb.homework.fragments.ui.FragmentBB

class FragmentBActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_fragment_b)

val fragmentBA = FragmentBA()
val fragmentBB = FragmentBB()

val orientation = resources.configuration.orientation
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
// supportFragmentManager.beginTransaction().add(R.id.fragment_container_ba, fragmentBA)
// .commit()
// supportFragmentManager.beginTransaction().add(R.id.fragment_container_bb, fragmentBB)
// .commit()

supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container_ba, fragmentBA)
.replace(R.id.fragment_container_bb, fragmentBB)
.commit()
}
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
supportFragmentManager.beginTransaction().add(R.id.activity_fragment_b, fragmentBA)
.commit()
}
}

companion object {
const val KEY_COLOR = "color"
const val KEY_REQUEST = "request_key"
}
}
30 changes: 30 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,42 @@
package otus.gpb.homework.fragments

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

class MainActivity : AppCompatActivity() {
private lateinit var buttonFragmentA: Button
private lateinit var buttonFragmentB: Button
private lateinit var fragmentA: FragmentA

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

buttonFragmentA = findViewById(R.id.button_fragment_a)
buttonFragmentB = findViewById(R.id.button_fragment_b)

buttonFragmentA.setOnClickListener {
fragmentA = FragmentA()
supportFragmentManager.beginTransaction()
.add(R.id.fragment_container, fragmentA)
.commit()
buttonFragmentA.visibility = View.GONE
buttonFragmentB.visibility = View.GONE
}

buttonFragmentB.setOnClickListener {
startActivity(Intent(this, FragmentBActivity::class.java))
}

supportFragmentManager.addOnBackStackChangedListener {
val count = supportFragmentManager.backStackEntryCount
if (count == 0) {
buttonFragmentA.visibility = View.VISIBLE
buttonFragmentB.visibility = View.VISIBLE
}
}
}
}
39 changes: 39 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/ui/FragmentBA.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package otus.gpb.homework.fragments.ui

import android.content.res.Configuration
import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.fragment.app.Fragment
import otus.gpb.homework.fragments.FragmentBActivity
import otus.gpb.homework.fragments.R


class FragmentBA : Fragment(R.layout.fragment_b_a) {


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

requireActivity().supportFragmentManager.setFragmentResultListener(
FragmentBActivity.KEY_REQUEST,
this
) { _, bundle ->
val result = bundle.getInt(FragmentBActivity.KEY_COLOR)
view.setBackgroundColor(result)
}

val orientation = resources.configuration.orientation
val button = view.findViewById<Button>(R.id.buttonOpenFragmentBB)
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
button.visibility = View.VISIBLE
button.text = "FragmentBB"
button.setOnClickListener() {
parentFragmentManager.beginTransaction()
.replace(R.id.activity_fragment_b, FragmentBB()).addToBackStack(null).commit()
}
} else {
button.visibility = View.GONE
}
}
}
41 changes: 41 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/ui/FragmentBB.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package otus.gpb.homework.fragments.ui

import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import otus.gpb.homework.fragments.ColorGenerator
import otus.gpb.homework.fragments.R


class FragmentBB : Fragment(R.layout.fragment_b_b) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

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

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

val button: Button = view.findViewById(R.id.buttonSendColor)

button.setOnClickListener() {
val bundle = Bundle()
bundle.putInt("color", ColorGenerator.generateColor())
requireActivity().supportFragmentManager
.setFragmentResult("request_key", bundle)
}
}
}
19 changes: 19 additions & 0 deletions app/src/main/res/layout-land/activity_fragment_b.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/fragment_container_ba"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />

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

</LinearLayout>
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_fragment_b.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?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:id="@+id/activity_fragment_b"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".FragmentBActivity">

</FrameLayout>
Loading