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
96 changes: 49 additions & 47 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}

kotlin {
jvmToolchain(17)
}

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

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

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '17'
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}

kotlin {
jvmToolchain(17)
}

android {
compileSdk 35
namespace "otus.gpb.homework.fragments"

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

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '17'
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
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.10.0'
implementation 'androidx.fragment:fragment-ktx:1.8.9'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
58 changes: 33 additions & 25 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Fragments"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Fragments"
tools:targetApi="31">
<activity
android:name=".Activity_Fragment_B"
android:exported="false"
android:label="ACTIVITY_B"/>
<activity
android:name=".Activity_Fragment_A"
android:exported="false"
android:label="ACTIVITY_A"/>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
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 Activity_Fragment_A : AppCompatActivity() {

lateinit private var fragmentA:Fragment_A

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


if (savedInstanceState == null) {
fragmentA = Fragment_A.newInstance()
supportFragmentManager.beginTransaction()
.replace( R.id.ContainerForFragments, fragmentA )
.addToBackStack("")
.commit()
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package otus.gpb.homework.fragments

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

class Activity_Fragment_B : AppCompatActivity() {

lateinit private var fragmentBA : FragmentBA

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

if( resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {

if (savedInstanceState == null) {
fragmentBA = FragmentBA.newInstance()
supportFragmentManager.beginTransaction()
.replace(R.id.ContainerForFragments, fragmentBA)
.addToBackStack("")
.commit()
}
else if( supportFragmentManager.backStackEntryCount == 0 )
finish()
}
}
}
52 changes: 52 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,52 @@
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

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

private var backGroundColor: Int? = null
private lateinit var startAB_Button:Button
private lateinit var fragmentAB: FragmentAB

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

startAB_Button = view.findViewById<Button>(R.id.StartFragmentAB_Button)

if( savedInstanceState != null ) {
backGroundColor = savedInstanceState.getInt(FRAGMENT_AA_COLOR_KEY)
} else {
backGroundColor = getArguments()?.getInt(FRAGMENT_AA_COLOR_KEY)
}
backGroundColor?.let{ view.setBackgroundColor(it) }

startAB_Button.setOnClickListener {

fragmentAB = FragmentAB.newInstance()
val bundle = Bundle()
bundle.putInt("SetColorAB", ColorGenerator.generateColor())
fragmentAB.setArguments(bundle)

parentFragmentManager.beginTransaction()
.replace(R.id.ContainerForFragments, fragmentAB)
.addToBackStack("")
.commit()
}
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
backGroundColor?.let{ outState.putInt( FRAGMENT_AA_COLOR_KEY, it) }
}

companion object {
@JvmStatic
fun newInstance() = FragmentAA()
}

}
34 changes: 34 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,34 @@
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

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

private var backGroundColor: Int? = null

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

if (savedInstanceState != null) {
backGroundColor = savedInstanceState.getInt( FRAGMENT_AB_COLOR_KEY )
} else {
backGroundColor = getArguments()?.getInt( FRAGMENT_AB_COLOR_KEY )
}
backGroundColor?.let { view.setBackgroundColor(it) }
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
backGroundColor?.let{ outState.putInt( FRAGMENT_AB_COLOR_KEY, it ) }
}

companion object {
@JvmStatic
fun newInstance( ) = FragmentAB()
}
}
62 changes: 62 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,62 @@
package otus.gpb.homework.fragments

import android.content.Context
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.activity.OnBackPressedCallback
import androidx.fragment.app.setFragmentResultListener

const val FRAGMENT_COLOR_KEY = "ColorBA"

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

private lateinit var startBB_Button: Button
private lateinit var fragmentBB: FragmentBB
private var backGroundColor: Int? = null

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

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

if(resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT){
backGroundColor = getArguments()?.getInt(FRAGMENT_COLOR_KEY)
backGroundColor?.let{ view.setBackgroundColor(it) }

startBB_Button = view.findViewById<Button>(R.id.StartFragmentBB_Button)
startBB_Button.setOnClickListener {
fragmentBB = FragmentBB.newInstance()
parentFragmentManager.beginTransaction()
.replace(R.id.ContainerForFragments, fragmentBB)
.addToBackStack("")
.commit()
}
}
else{
setFragmentResultListener(FRAGMENT_COLOR_KEY) { _, bundle ->
backGroundColor = bundle.getInt(FRAGMENT_COLOR_KEY)
backGroundColor?.let{ view.setBackgroundColor(it) } }
}
}

companion object {
@JvmStatic
fun newInstance() = FragmentBA()
}
}
Loading