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'
}
buildFeatures {
viewBinding 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.fragment:fragment-ktx:1.8.5'
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
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ActivityA"
android:launchMode="standard"
android:exported="false">
</activity>
<activity
android:name=".ActivityB"
android:launchMode="standard"
android:exported="false">
</activity>
</application>

</manifest>
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.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class ActivityA: AppCompatActivity(R.layout.activity_a) {

@SuppressLint("ResourceType")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportFragmentManager.beginTransaction()
.add(R.id.containerActivityA, FragmentMainActivityA())
.commit()
}
}
26 changes: 26 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,26 @@
package otus.gpb.homework.fragments

import android.content.res.Configuration
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class ActivityB: AppCompatActivity() {

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

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_b)
val fm = supportFragmentManager.beginTransaction()
fm.add(R.id.containerBA, fragmentBB)
fm.add(R.id.containerBB, fragmentBA)
if(resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
fm.hide(fragmentBB)
}
fm.commit()
}

fun getFragmentBA() = fragmentBA
fun getFragmentBB() = fragmentBB
}
41 changes: 41 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,41 @@
package otus.gpb.homework.fragments

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.View
import android.widget.Button
import androidx.activity.OnBackPressedCallback
import androidx.core.os.bundleOf
import androidx.fragment.app.setFragmentResult

class FragmentA : Fragment(R.layout.fragment_a) {

override fun onAttach(context: Context) {
super.onAttach(context)
val backPressedCallBack = object : OnBackPressedCallback(true)
{
override fun handleOnBackPressed() {
if(parentFragmentManager.backStackEntryCount <= 1)
requireActivity().finish()
}
}
requireActivity().onBackPressedDispatcher.addCallback(this, backPressedCallBack)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val button = view.findViewById<Button>(R.id.buttonOpenFragmentAA)
button.setOnClickListener{
val bundle = Bundle()
bundle.putInt("toFragmentAA", ColorGenerator.generateColor())
val fragment = FragmentAA()
fragment.setArguments(bundle)

parentFragmentManager.beginTransaction()
.replace(R.id.containerActivityA, fragment)
.addToBackStack("")
.commit()
}
}
}
42 changes: 42 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,42 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.View
import android.widget.Button
import androidx.core.os.bundleOf
import androidx.fragment.app.setFragmentResult
import androidx.fragment.app.setFragmentResultListener

class FragmentAA : Fragment(R.layout.fragment_aa) {

private var color: Int? = null

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

if( savedInstanceState != null ) {
color = savedInstanceState.getInt("color")
} else {
color = getArguments()?.getInt("toFragmentAA")
}
color?.let{ view.setBackgroundColor(it) }

val button = view.findViewById<Button>(R.id.buttonOpenFragmentAB)
button.setOnClickListener(){
val bundle = Bundle()
bundle.putInt("colorToFragmentAB", ColorGenerator.generateColor())
val fragment = FragmentAB()
fragment.setArguments(bundle)
parentFragmentManager.beginTransaction()
.replace(R.id.containerActivityA, fragment)
.addToBackStack("")
.commit()
}
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
color?.let{ outState.putInt("color", it) }
}
}
27 changes: 27 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,27 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.View
import androidx.fragment.app.setFragmentResultListener

class FragmentAB : Fragment(R.layout.fragment_ab) {

private var color: Int? = null

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

if(savedInstanceState != null) {
color = savedInstanceState.getInt("color")
} else {
color = getArguments()?.getInt("colorToFragmentAB")
}
color?.let{view.setBackgroundColor(it)}
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
color?.let{ outState.putInt("color", it) }
}
}
41 changes: 41 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,41 @@
package otus.gpb.homework.fragments

import android.content.res.Configuration
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.View
import android.widget.Button
import androidx.fragment.app.setFragmentResult
import androidx.core.os.bundleOf
import androidx.fragment.app.setFragmentResultListener

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

private var color: Int = 0
private lateinit var button: Button

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
button = view.findViewById<Button>(R.id.buttonOpenFragmentBB)
button.setOnClickListener() {
setFragmentResult("toFragmentBB", bundleOf("color" to color))
val fm = parentFragmentManager.beginTransaction()
if(resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
fm.hide((requireActivity() as ActivityB).getFragmentBA())
}
fm.show((requireActivity() as ActivityB).getFragmentBB())
fm.commit()
}
setColorOfButton()
setFragmentResultListener("toFragmentBA") { _, bundle ->
val color = bundle.getInt("color")
getView()?.setBackgroundColor(color)
setColorOfButton()
}
}

private fun setColorOfButton(){
color = ColorGenerator.generateColor()
button.setBackgroundColor(color)
}
}
41 changes: 41 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,41 @@
package otus.gpb.homework.fragments

import android.content.res.Configuration
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.View
import android.widget.Button
import androidx.fragment.app.setFragmentResult
import androidx.core.os.bundleOf
import androidx.fragment.app.setFragmentResultListener

class FragmentBB : Fragment(R.layout.fragment_bb) {

private var color: Int = 0
private lateinit var button: Button

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
button = view.findViewById<Button>(R.id.buttonOpenFragmentBA)
button.setOnClickListener(){
setFragmentResult("toFragmentBA", bundleOf("color" to color))
val fm = parentFragmentManager.beginTransaction()
if(resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
fm.hide((requireActivity() as ActivityB).getFragmentBB())
}
fm.show((requireActivity() as ActivityB).getFragmentBA())
fm.commit()
}
setColorOfButton()
setFragmentResultListener("toFragmentBB") { _, bundle ->
val color = bundle.getInt("color")
getView()?.setBackgroundColor(color)
setColorOfButton()
}
}

private fun setColorOfButton(){
color = ColorGenerator.generateColor()
button.setBackgroundColor(color)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.View
import android.widget.Button

class FragmentMainActivityA : Fragment(R.layout.fragment_main_activity_a) {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val button = view.findViewById<Button>(R.id.buttonOpenFragmentA)
button.setOnClickListener(){
parentFragmentManager.beginTransaction()
.replace(R.id.containerActivityA, FragmentA())
.addToBackStack("")
.commit()
}
}
}
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)
binding.buttonFragmentA.setOnClickListener {
val intent = Intent(this, ActivityA::class.java)
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
binding.buttonFragmentB.setOnClickListener {
val intent = Intent(this, ActivityB::class.java)
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
}
}
}


20 changes: 20 additions & 0 deletions app/src/main/res/layout-land/activity_b.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:baselineAligned="false"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

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

</LinearLayout>
12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_a.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

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

</androidx.constraintlayout.widget.ConstraintLayout>
Loading