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

kotlin {
jvmToolchain(17)
}
//kotlin {
// jvmToolchain(17)
//}

android {
compileSdk 34
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".ActivityA"
android:exported="false"
android:launchMode="standard"
/>

<activity
android:name=".ActivityB"
android:exported="false"
android:launchMode="standard"
/>

</application>

</manifest>
26 changes: 26 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,26 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity

class ActivityA : AppCompatActivity() {

private lateinit var fragmentA: FragmentA

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

if (savedInstanceState == null) {

fragmentA = FragmentA()

supportFragmentManager.beginTransaction()
.replace(R.id.fragmentA_container, fragmentA)
.addToBackStack(null)
.commit()
}

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

import android.content.res.Configuration
import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import java.util.Objects

class ActivityB : AppCompatActivity() {

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

// ландшафтный режим - отобразить оба фрагмента одновременно
if (isLandscape()) {
findViewById<FrameLayout>(R.id.frameLayout_bb).visibility = View.VISIBLE
supportFragmentManager.beginTransaction()
.replace(R.id.frameLayout_ba, FragmentBA())
.replace(R.id.frameLayout_bb, FragmentBB())
.commit()
}
else {
// портретный режим режим - отобразить только один фрагмент
findViewById<FrameLayout>(R.id.frameLayout_bb).visibility = View.GONE
supportFragmentManager.beginTransaction()
.replace(R.id.frameLayout_ba, FragmentBA())
.commit()
}
}

private fun isLandscape() =
Objects.equals(Configuration.ORIENTATION_LANDSCAPE, resources.configuration.orientation)
}
49 changes: 49 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,49 @@
package otus.gpb.homework.fragments

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() {

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
val view = inflater.inflate(R.layout.fragment_a, container, false)
val openFragmentAAButton = view.findViewById<Button>(R.id.buttonOpenFragmentAA)

requireActivity().onBackPressedDispatcher.addCallback(
viewLifecycleOwner,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (childFragmentManager.backStackEntryCount > 0) {
childFragmentManager.popBackStack()
} else {
requireActivity().finish()
}
}
}
)

openFragmentAAButton.setOnClickListener {
val fragmentAa = FragmentAA()
val args = Bundle()
args.putInt("color", ColorGenerator.generateColor())
fragmentAa.arguments = args

childFragmentManager.beginTransaction()
.add(R.id.frame_layout_fragment_a, fragmentAa)
.addToBackStack(null)
.commit()
}

return view
}
}
36 changes: 36 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,36 @@
package otus.gpb.homework.fragments

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment

class FragmentAA : Fragment() {

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
val view = inflater.inflate(R.layout.fragment_aa, container, false)

arguments?.getInt("color")?.let { view.setBackgroundColor(it) }

view.findViewById<Button>(R.id.button_openFragmentAB).setOnClickListener {
val fragmentAb = FragmentAB()
val args = Bundle()
args.putInt("color", ColorGenerator.generateColor())
fragmentAb.arguments = args

parentFragmentManager.beginTransaction()
.replace(R.id.frame_layout_fragment_a, fragmentAb)
.addToBackStack(null)
.commit()
}

return view
}
}
22 changes: 22 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,22 @@
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

class FragmentAB : Fragment() {

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
val view = inflater.inflate(R.layout.fragment_ab, container, false)

arguments?.getInt("color")?.let { view.setBackgroundColor(it) }

return view
}
}
58 changes: 58 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,58 @@
package otus.gpb.homework.fragments

import android.content.res.Configuration
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment
import java.util.Objects

private val KEY_COLOR = "key_color"

class FragmentBA: Fragment() {

private var color: Int = 0

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?, ): View? {
val view = inflater.inflate(R.layout.fragment_ba, container, false)

parentFragmentManager.setFragmentResultListener(KEY_COLOR, this) { _, bundle ->
color = bundle.getInt(KEY_COLOR)
view.setBackgroundColor(color)
}

val openFragBB = view.findViewById<Button>(R.id.buttonOpenFragmentBB)

// в ландшафтном режиме кнопки "Открыть BB" не должно быть
openFragBB.visibility = when (isLandscape()) {
true -> View.GONE
false -> View.VISIBLE
}

// в портретном режиме при нажатии на кнопку вместо BA открыть BB
openFragBB.setOnClickListener {
parentFragmentManager.beginTransaction()
.replace(R.id.frameLayout_ba, FragmentBB())
.addToBackStack(null)
.commit()
}



return view
}

private fun isLandscape(): Boolean =
Objects.equals(Configuration.ORIENTATION_LANDSCAPE, resources.configuration.orientation)

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt(KEY_COLOR, color)
}

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

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

class FragmentBB: Fragment() {

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
val view = inflater.inflate(R.layout.fragment_bb, container, false)

val fragmentBA = FragmentBA()
view.findViewById<Button>(R.id.buttonSendColor)
.setOnClickListener {
parentFragmentManager.beginTransaction()
.replace(R.id.frameLayout_ba, fragmentBA).apply {
val args = Bundle()
args.putInt("key_color", ColorGenerator.generateColor())
fragmentBA.arguments = args
parentFragmentManager.setFragmentResult("key_color", args)
}
.commit()
}

return view
}
}
10 changes: 10 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,22 @@
package otus.gpb.homework.fragments

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

class MainActivity : AppCompatActivity() {

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

findViewById<Button>(R.id.openActivityA).setOnClickListener {
startActivity(Intent(this, ActivityA::class.java))
}

findViewById<Button>(R.id.openActivityB).setOnClickListener {
startActivity(Intent(this, ActivityB::class.java))
}
}
}
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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityA">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentA_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/material_dynamic_neutral70"
/>

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

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

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

</LinearLayout>
21 changes: 21 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,25 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/openActivityA"
android:layout_width="200dp"
android:layout_height="48dp"
app:layout_constraintBottom_toTopOf="@+id/openActivityB"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:text="Open Activity A"
/>

<Button
android:id="@+id/openActivityB"
android:layout_width="200dp"
android:layout_height="48dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:text="Open Activity B"
/>

</androidx.constraintlayout.widget.ConstraintLayout>
Loading