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
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ 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'
implementation 'com.google.android.material:material:1.12.0'
}
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
android:supportsRtl="true"
android:theme="@style/Theme.Fragments"
tools:targetApi="31">
<activity
android:name=".Activity2"
android:exported="false"
android:label="Activity2" />
<activity
android:name=".Activity1"
android:exported="false"
android:label="Activity1" />
<activity
android:name=".MainActivity"
android:exported="true">
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/Activity1.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 androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class Activity1 : AppCompatActivity() {
// private lateinit var fragmentA: FragmentA

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_1)
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
}
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container, FragmentA(), "FragmentA")
.addToBackStack(null)
.commit()
}
}
}
40 changes: 40 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/Activity2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package otus.gpb.homework.fragments

import android.content.res.Configuration
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class Activity2 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_2)
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
}
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container, FragmentBA(), "FragmentBA")
.commit()
}
val orientation = resources.configuration.orientation
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
val fc = findViewById<androidx.fragment.app.FragmentContainerView>(R.id.fragment_container2)
fc.visibility = View.GONE
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container2, FragmentBB(), "FragmentBB")
.commit()
}
}
}
}
47 changes: 47 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,47 @@
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 android.widget.Button

/**
* A simple [Fragment] subclass.
* Use the [FragmentA.newInstance] factory method to
* create an instance of this fragment.
*/
class FragmentA : Fragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {

// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_a, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.button).setOnClickListener {
if (savedInstanceState == null) {
val color = ColorGenerator.generateColor()
childFragmentManager.beginTransaction()
.replace(R.id.fragment_a_container, FragmentAA.newInstance(color), "FragmentAA")
.addToBackStack(null)
.commit()
}
}
view.findViewById<Button>(R.id.button_back).setOnClickListener {
if (childFragmentManager.backStackEntryCount > 0) {
childFragmentManager.popBackStack()
} else {
requireActivity().finish()
}
}
}
}
56 changes: 56 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,56 @@
package otus.gpb.homework.fragments

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

private const val ARG_COLOR = "color"

class FragmentAA : Fragment() {
private var color: Int? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
color = it.getInt(ARG_COLOR)
}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_a_a, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.button).setOnClickListener {
if (savedInstanceState == null) {
val color = ColorGenerator.generateColor()
parentFragmentManager.beginTransaction()
.replace(R.id.fragment_a_container, FragmentAB.newInstance(color), "FragmentAB")
.addToBackStack(null)
.commit()
}
}
val v = view.findViewById<androidx.constraintlayout.widget.ConstraintLayout>(R.id.fragment_a_a)
if (color != null)
v.setBackgroundColor(color!!)
}

companion object {
@JvmStatic
fun newInstance(color: Int) =
FragmentAA().apply {
arguments = Bundle().apply {
putInt(ARG_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 androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

private const val ARG_COLOR = "color"
/**
* A simple [Fragment] subclass.
* Use the [FragmentAB.newInstance] factory method to
* create an instance of this fragment.
*/
class FragmentAB : Fragment() {
private var color: Int? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
color = it.getInt(ARG_COLOR)
}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_a_b, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val v = view.findViewById<androidx.constraintlayout.widget.ConstraintLayout>(R.id.fragment_a_b)
if (color != null)
v.setBackgroundColor(color!!)
}

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

import android.content.Context
import android.content.res.Configuration
import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.core.os.bundleOf

const val RESULT_KEY = "RESULT_KEY"
const val RESULT_BUNDLE_KEY = "RESULT_BUNDLE_KEY"
/**
* A simple [Fragment] subclass.
* Use the [FragmentBA.newInstance] factory method to
* create an instance of this fragment.
*/
class FragmentBA : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_b_a, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.buttonOpenBB).setOnClickListener {
if (savedInstanceState == null) {
parentFragmentManager.beginTransaction()
.replace(R.id.fragment_container, FragmentBB(), "FragmentBB")
.addToBackStack(null)
.commit()
}
}
parentFragmentManager.setFragmentResultListener(RESULT_KEY, this) { _, bundle ->
val color = bundle.getInt(RESULT_BUNDLE_KEY)
val v = view.findViewById<androidx.constraintlayout.widget.ConstraintLayout>(R.id.fragmentBA)
v.setBackgroundColor(color)
}
val orientation = resources.configuration.orientation
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
val bt = view.findViewById<Button>(R.id.buttonOpenBB)
bt.visibility = View.GONE
}
}
}
43 changes: 43 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,43 @@
package otus.gpb.homework.fragments

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


/**
* A simple [Fragment] subclass.
* Use the [FragmentBB.newInstance] factory method to
* create an instance of this fragment.
*/
class FragmentBB : Fragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_b_b, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.buttonSendColor).setOnClickListener {
val color = ColorGenerator.generateColor()
parentFragmentManager.setFragmentResult(RESULT_KEY, bundleOf(RESULT_BUNDLE_KEY to color))
val orientation = resources.configuration.orientation
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
parentFragmentManager.popBackStack()
}
}
}
}
8 changes: 8 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,20 @@
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.button1).setOnClickListener {
startActivity(Intent(this, Activity1::class.java))
}
findViewById<Button>(R.id.button2).setOnClickListener {
startActivity(Intent(this, Activity2::class.java))
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_arrow_back_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:autoMirrored="true" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z"/>

</vector>
Loading