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

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

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

Expand All @@ -27,6 +27,9 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
buildFeatures {
viewBinding = true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
Expand All @@ -45,4 +48,5 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'androidx.activity:activity:1.10.1'
}
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ActivityB"
android:exported="false" />
<activity
android:name=".ActivityA"
android:exported="false" />
</application>

</manifest>
32 changes: 32 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,32 @@
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 ActivityA : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_a)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.containerA)) { 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.containerA, 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 android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
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()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Транзакция с addToBackStack(null) сама управляет стеком

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()
}
}
}
55 changes: 55 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,55 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
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)
}
}
}
}
}
47 changes: 47 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,47 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
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)
}
}
}
}
}
52 changes: 52 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,52 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Давайте проверять resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE. Через binding.containerFragmentB == null может работать, но ненадежно :)

childFragmentManager.beginTransaction()
.replace(R.id.containerFragmentBA, FragmentBA.newInstance())
.commit()
childFragmentManager.beginTransaction()
.replace(R.id.containerFragmentBB, 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()
}
}
}
51 changes: 51 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,51 @@
package otus.gpb.homework.fragments

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

class FragmentBA : Fragment() {

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

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

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
parentFragmentManager.setFragmentResultListener(FragmentBB.KEY_BB, this){ key, bundle ->
val color = bundle.getInt(FragmentBB.BK_COLOR)
binding.layoutFragmentBA.setBackgroundColor(color)
}
if(binding.buttonFragmentBB != null){
binding.buttonFragmentBB?.setOnClickListener(){
parentFragmentManager.beginTransaction()
.replace(R.id.containerFragmentB, FragmentBB.newInstance())
.addToBackStack(null)
.commit()
}
}
}

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

companion object {

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