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
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ plugins {
id 'org.jetbrains.kotlin.android'
}

kotlin {
jvmToolchain(17)
}

android {
compileSdk 34
namespace "otus.gpb.homework.fragments"
Expand Down Expand Up @@ -34,6 +30,9 @@ android {
kotlinOptions {
jvmTarget = '17'
}
viewBinding {
enabled = true
}
}

dependencies {
Expand All @@ -42,6 +41,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
28 changes: 28 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,28 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import android.window.OnBackInvokedDispatcher
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import otus.gpb.homework.fragments.databinding.ActivityABinding
import otus.gpb.homework.fragments.databinding.ActivityMainBinding
import otus.gpb.homework.fragments.fragments.FragmentA

class ActivityA : AppCompatActivity() {

private lateinit var binding: ActivityABinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
binding = ActivityABinding.inflate(layoutInflater)
setContentView(binding.root)

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

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import otus.gpb.homework.fragments.databinding.ActivityBBinding
import otus.gpb.homework.fragments.fragments.FragmentBA
import otus.gpb.homework.fragments.fragments.FragmentBB

class ActivityB : AppCompatActivity() {

private lateinit var binding: ActivityBBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
binding = ActivityBBinding.inflate(layoutInflater)
setContentView(binding.root)

supportFragmentManager.beginTransaction()
.replace(R.id.fragmentContainerBA, FragmentBA())
.replace(R.id.fragmentContainerBB, FragmentBB())
.commit()

switchFragments()
}

private fun switchFragments() {

if (ScreenOrientationHelper.getScreenOrientation(resources) == "PORTRAIT") {
with(binding) {
fragmentContainerBA.isVisible = true
fragmentContainerBB.isVisible = false
}

} else {
with(binding) {
fragmentContainerBA.isVisible = true
fragmentContainerBB.isVisible = true
}
}
}
}
20 changes: 18 additions & 2 deletions app/src/main/java/otus/gpb/homework/fragments/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
package otus.gpb.homework.fragments

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import otus.gpb.homework.fragments.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

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

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

with(binding) {
openActivityA.setOnClickListener {
startActivity(Intent(applicationContext, ActivityA::class.java))
}
openActivityB.setOnClickListener {
startActivity(Intent(applicationContext, ActivityB::class.java))
}
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package otus.gpb.homework.fragments

import android.content.res.Configuration
import android.content.res.Resources

object ScreenOrientationHelper {

fun getScreenOrientation(resources: Resources): String {
return when (resources.configuration.orientation) {
Configuration.ORIENTATION_PORTRAIT -> "PORTRAIT"
Configuration.ORIENTATION_LANDSCAPE -> "LANDSCAPE"
else -> ""
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package otus.gpb.homework.fragments.fragments

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 FragmentA : Fragment(R.layout.fragment_a) {

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

val bundle = Bundle()
bundle.putInt("color", ColorGenerator.generateColor())
val fragmentAA = FragmentAA()
fragmentAA.arguments = bundle

view.findViewById<Button>(R.id.openFragmentAA).setOnClickListener {
childFragmentManager.beginTransaction()
.add(R.id.fragmentContainerAA, fragmentAA)
.addToBackStack("fragmentA")
.commit()
}

val callback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
activity?.finish()
}
}
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, callback)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package otus.gpb.homework.fragments.fragments

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 FragmentAA : Fragment(R.layout.fragment_a_a) {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val color = arguments?.getInt("color")

if (color != null) {
this.view?.setBackgroundColor(color)
}

val bundle = Bundle()
bundle.putInt("color", ColorGenerator.generateColor())
val fragmentAB = FragmentAB()
fragmentAB.arguments = bundle

view.findViewById<Button>(R.id.openFragmentAB).setOnClickListener {

parentFragmentManager.beginTransaction()
.replace(R.id.fragmentContainerAB, fragmentAB)
.addToBackStack("fragmentAA")
.commit()
}

val callback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
parentFragmentManager.popBackStack()
}
}
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, callback)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package otus.gpb.homework.fragments.fragments

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

class FragmentAB : Fragment(R.layout.fragment_a_b) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val color = arguments?.getInt("color")

if (color != null) {
this.view?.setBackgroundColor(color)
}

val callback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
parentFragmentManager.popBackStack("fragmentAA", 1)
}
}
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, callback)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package otus.gpb.homework.fragments.fragments

import androidx.fragment.app.Fragment
import otus.gpb.homework.fragments.R

class FragmentB : Fragment(R.layout.fragment_b)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package otus.gpb.homework.fragments.fragments

import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import otus.gpb.homework.fragments.R
import otus.gpb.homework.fragments.ScreenOrientationHelper

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

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

parentFragmentManager.setFragmentResultListener("colorBundle", this) { _, bundle ->
val color = bundle.getInt("color")
this.view?.setBackgroundColor(color)
}

with(view.findViewById<Button>(R.id.openFragmentBB)) {

setOnClickListener {
parentFragmentManager.beginTransaction()
.replace(R.id.fragmentContainerBA, FragmentBB())
.commit()
}

isVisible = ScreenOrientationHelper.getScreenOrientation(resources) == "PORTRAIT"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package otus.gpb.homework.fragments.fragments

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

class FragmentBB : Fragment(R.layout.fragment_b_b) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

view.findViewById<Button>(R.id.sendColor).setOnClickListener {

val bundle = Bundle()
bundle.putInt("color", ColorGenerator.generateColor())

parentFragmentManager.setFragmentResult("colorBundle", bundle)

if (ScreenOrientationHelper.getScreenOrientation(resources) == "PORTRAIT") {
parentFragmentManager.beginTransaction()
.add(R.id.fragmentContainerBA, FragmentBA())
.commit()
}
}
}
}
15 changes: 15 additions & 0 deletions app/src/main/res/layout/activity_a.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityA">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainer"

android:layout_width="match_parent"
android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Loading