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: 6 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ kotlin {
}

android {
compileSdk 34
compileSdk 35
namespace "otus.gpb.homework.fragments"

defaultConfig {
Expand All @@ -34,6 +34,10 @@ android {
kotlinOptions {
jvmTarget = '17'
}

buildFeatures {
viewBinding = true
}
}

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

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment

class ActivityA : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_a)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.container_a)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.container_a, FragmentA.newInstance())
.commit()
}
}

companion object {
fun newIntent(context: Context): Intent{
return Intent(context, ActivityA::class.java)
}
}
}
33 changes: 33 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,33 @@
package otus.gpb.homework.fragments

import android.content.Context
import android.content.Intent
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.container_b)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
if (savedInstanceState == null){
supportFragmentManager.beginTransaction()
.replace(R.id.container_b, FragmentB.newInstance())
.commit()
}
}

companion object {
fun newIntent(context: Context): Intent {
return Intent(context, ActivityB::class.java)
}
}
}
55 changes: 55 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,55 @@
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 androidx.activity.OnBackPressedCallback
import otus.gpb.homework.fragments.databinding.FragmentABinding

class FragmentA : Fragment() {

private var _binding: FragmentABinding? = null
private val binding: FragmentABinding
get() = _binding ?: throw RuntimeException("FragmentABinding == null")

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentABinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, object : OnBackPressedCallback(true){
override fun handleOnBackPressed() {
if (childFragmentManager.backStackEntryCount <= 0){
requireActivity().finish()
} else {
childFragmentManager.popBackStack()
}
}
})
binding.buttonFragmentAA.setOnClickListener(){
childFragmentManager.popBackStack()
childFragmentManager.beginTransaction()
.replace(R.id.containerFragmentA, FragmentAA.newInstance(ColorGenerator.generateColor()))
.addToBackStack(null)
.commit()
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

companion object {
fun newInstance(): FragmentA{
return FragmentA()
}
}
}
63 changes: 63 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,63 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import otus.gpb.homework.fragments.databinding.FragmentAABinding

class FragmentAA : Fragment() {

private var _binding: FragmentAABinding? = null
private val binding: FragmentAABinding
get() = _binding ?: throw RuntimeException("FragmentAABinding == null")
private var colorInt: Int? = 0


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
colorInt = arguments?.getInt(COLOR)
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentAABinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.buttonFragmentAB.setOnClickListener(){
parentFragmentManager.beginTransaction()
.replace(R.id.containerFragmentA, FragmentAB.newInstance(ColorGenerator.generateColor()))
.addToBackStack(null)
.commit()
}
colorInt?.let {
binding.layoutFragmentAA.setBackgroundColor(it)
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

companion object {

private const val COLOR = "color"

fun newInstance(color: Int): FragmentAA {
return FragmentAA().apply {
arguments = Bundle().apply {
putInt(COLOR, color)
}
}
}
}

}
56 changes: 56 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,56 @@
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 otus.gpb.homework.fragments.FragmentAA.Companion
import otus.gpb.homework.fragments.databinding.FragmentAABinding
import otus.gpb.homework.fragments.databinding.FragmentABBinding

class FragmentAB : Fragment() {

private var _binding: FragmentABBinding? = null
private val binding: FragmentABBinding
get() = _binding ?: throw RuntimeException("FragmentAABinding == null")
private var colorInt: Int? = 0

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
colorInt = arguments?.getInt(COLOR)
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentABBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
colorInt?.let {
binding.layoutFragmentAB.setBackgroundColor(it)
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

companion object {

private const val COLOR = "color"

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

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.FragmentContainerView
import otus.gpb.homework.fragments.databinding.FragmentBBinding

class FragmentB : Fragment() {

private var _binding: FragmentBBinding? = null
private val binding: FragmentBBinding
get() = _binding ?: throw RuntimeException("FragmentAABinding == null")

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentBBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (binding.containerFragmentB == null) {
childFragmentManager.beginTransaction()
.replace(R.id.containerFragmentB1, FragmentBA.newInstance())
.commit()
childFragmentManager.beginTransaction()
.replace(R.id.containerFragmentB2, FragmentBB.newInstance())
.commit()
} else {
childFragmentManager.beginTransaction()
.replace(R.id.containerFragmentB, FragmentBA.newInstance())
.addToBackStack(null)
.commit()
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

companion object {

fun newInstance(): FragmentB{
return FragmentB()
}
}
}
Loading