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
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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.8.0'
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
29 changes: 29 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,29 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import androidx.activity.addCallback
import androidx.appcompat.app.AppCompatActivity

class ActivityA : AppCompatActivity() {

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

if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.container_a, FragmentA(), "FragmentA_Tag").commit()
}

onBackPressedDispatcher.addCallback(this) {
val fragmentA = supportFragmentManager.findFragmentByTag("FragmentA_Tag")
val childStackCount = fragmentA?.childFragmentManager?.backStackEntryCount ?: 0

if (childStackCount > 0) {
fragmentA?.childFragmentManager?.popBackStack()
} else {
finish()
}
}
}
}
20 changes: 20 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,20 @@
package otus.gpb.homework.fragments

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.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
}
}
30 changes: 30 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,30 @@
package otus.gpb.homework.fragments

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

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

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

val buttonOpenAA = view.findViewById<Button>(R.id.btnOpenAA)

buttonOpenAA.setOnClickListener {
val fragmentAA = FragmentAA()

val color = ColorGenerator.generateColor()

val args = Bundle()
args.putInt("color_key", color)
fragmentAA.arguments = args

childFragmentManager.beginTransaction()
.replace(R.id.child_container, fragmentAA)
.addToBackStack(null)
.commit()
}
}
}
28 changes: 28 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,28 @@
package otus.gpb.homework.fragments

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

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

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

val buttonOpenAB = view.findViewById<Button>(R.id.btnOpenAB)
buttonOpenAB.setOnClickListener {
val fragmentAB = FragmentAB()
val bundle = Bundle()
bundle.putInt("color_key", ColorGenerator.generateColor())
fragmentAB.arguments = bundle

parentFragmentManager.beginTransaction()
.replace(R.id.child_container, fragmentAB)
.addToBackStack(null)
.commit()
}
}
}
13 changes: 13 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,13 @@
package otus.gpb.homework.fragments

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

class FragmentAB : Fragment(R.layout.fragment_ab) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val color = arguments?.getInt("color_key") ?: 0
view.setBackgroundColor(color)
}
}
31 changes: 31 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,31 @@
package otus.gpb.homework.fragments

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

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

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

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

btnOpenBB?.setOnClickListener {
parentFragmentManager.beginTransaction()
.replace(R.id.container_b, FragmentBB())
.addToBackStack(null)
.commit()
}

parentFragmentManager.setFragmentResultListener("color_request", viewLifecycleOwner) { _, bundle ->
val color = bundle.getInt("color_result")
view.setBackgroundColor(color)
}

if (activity?.findViewById<View>(R.id.container_bb) != null) {
btnOpenBB?.visibility = View.GONE
}
}
}
26 changes: 26 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,26 @@
package otus.gpb.homework.fragments

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

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

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

val btnGenerate = view.findViewById<Button>(R.id.btnGenerateColor)

btnGenerate.setOnClickListener {
val color = ColorGenerator.generateColor()
val result = Bundle().apply { putInt("color_result", color) }

parentFragmentManager.setFragmentResult("color_request", result)

if (activity?.findViewById<View>(R.id.container_b) != null) {
parentFragmentManager.popBackStack()
}
}
}
}
16 changes: 16 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,28 @@
package otus.gpb.homework.fragments

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

class MainActivity : AppCompatActivity() {

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

val btnTask1 = findViewById<Button>(R.id.btnTask1)
val btnTask2 = findViewById<Button>(R.id.btnTask2)

btnTask1.setOnClickListener {
val intent = Intent(this, ActivityA::class.java)
startActivity(intent)
}

btnTask2.setOnClickListener {
val intent = Intent(this, ActivityB::class.java)
startActivity(intent)
}
}
}
27 changes: 27 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,27 @@
<?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">

<LinearLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/container_ba"
android:name="otus.gpb.homework.fragments.FragmentBA"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />

<androidx.fragment.app.FragmentContainerView
android:id="@+id/container_bb"
android:name="otus.gpb.homework.fragments.FragmentBB"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_a.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container_a"
android:layout_width="match_parent"
android:layout_height="match_parent" />
16 changes: 16 additions & 0 deletions app/src/main/res/layout/activity_b.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityB">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/container_b"
android:name="otus.gpb.homework.fragments.FragmentBA"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
21 changes: 18 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,28 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
<Button
android:id="@+id/btnTask1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:text="Задание 1"
app:layout_constraintBottom_toTopOf="@+id/btnTask2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />

<Button
android:id="@+id/btnTask2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Задание 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toBottomOf="@+id/btnTask1" />

</androidx.constraintlayout.widget.ConstraintLayout>
30 changes: 30 additions & 0 deletions app/src/main/res/layout/fragment_a.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Fragment A"
android:textSize="24sp" />

<Button
android:id="@+id/btnOpenAA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Открыть Fragment AA" />
</LinearLayout>

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

</FrameLayout>
24 changes: 24 additions & 0 deletions app/src/main/res/layout/fragment_aa.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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"
android:gravity="center"
android:orientation="vertical"
android:padding="16dp"
tools:context=".FragmentAA">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Fragment AA"
android:textSize="24sp" />

<Button
android:id="@+id/btnOpenAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To Fragment AB" />

</LinearLayout>
Loading