Skip to content
Closed
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: 5 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ android {

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'

implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.activity:activity-ktx:1.5.1'
implementation 'androidx.fragment:fragment-ktx:1.5.2'
implementation 'com.squareup.picasso:picasso:2.71828'
}
19 changes: 18 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,32 @@
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>

<activity
android:name=".MainActivity_A"
android:exported="true">
<meta-data
android:name="android.app.lib_name"
android:value="" />

</activity>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно добавить еще один активити к примеру сделать 2 кнопки с переходом MainActivity на MainActivity_A и MainActivity на MainActivity_B


<activity
android:name=".MainActivity_B"
android:exported="true">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>

</manifest>
50 changes: 50 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/Fragment_A.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package otus.gpb.homework.fragments

import android.content.Context
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 Fragment_A : Fragment() {

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.OpenFragment_AA).setOnClickListener{
val Fr_AA = Fragment_AA()
parentFragmentManager.beginTransaction()
.replace(R.id.FrameLayout_AA, Fr_AA, "Fr_AA")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно использовать parentFragmentManager, так он даст выполнить еще условие пункт 4 в задании

.addToBackStack(null)
.commit()
}
}
override fun onAttach(context: Context) {
super.onAttach(context)
val callback: OnBackPressedCallback =
object : OnBackPressedCallback(true)
{
override fun handleOnBackPressed() {
if(parentFragmentManager.fragments.count() > 1 )
parentFragmentManager.popBackStack()
else
requireActivity().finish()
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут можно проверить сколько фрагментов остался во фрагмент менеджере как описано в пункте 4 задачи
примерно так
if(количество фрагментов > 1 )
parentFragmentManager.popBackStack()
else
requireActivity().finish()

}
requireActivity().onBackPressedDispatcher.addCallback(
this,
callback
)
}

}
32 changes: 32 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/Fragment_AA.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 Fragment_AA : Fragment() {

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)
this.view?.setBackgroundColor(ColorGenerator.generateColor())
view.findViewById<Button>(R.id.OpenFragment_AB).setOnClickListener{
val Fr_AB = Fragment_AB()
parentFragmentManager.
beginTransaction()
.replace(R.id.FrameLayout_AA, Fr_AB, "Fr_AB")
.addToBackStack(null)
.commit()
}
}
}
23 changes: 23 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/Fragment_AB.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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 Fragment_AB : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
this.view?.setBackgroundColor(ColorGenerator.generateColor())
}
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)
}

}
41 changes: 41 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/Fragment_BA.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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
import androidx.fragment.app.setFragmentResultListener

class Fragment_BA : Fragment() {


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)

setFragmentResultListener("Color") { key, bundle ->
val color = bundle.getInt("COLOR")
this.view?.setBackgroundColor(color)
}
if (this.view?.findViewById<Button>(R.id.OpenFragment_BB) != null)
{
view.findViewById<Button>(R.id.OpenFragment_BB).setOnClickListener{
val Fr_BB = Fragment_BB()
parentFragmentManager.
beginTransaction()
.replace(R.id.Fr_Port, Fr_BB, "Fr_BB")
.addToBackStack(null)
.commit()
}
}
}
}

33 changes: 33 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/Fragment_BB.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.setFragmentResult

class Fragment_BB : Fragment() {

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.sendColor).setOnClickListener()
{
val color = ColorGenerator.generateColor()
val bundle = bundleOf(
"COLOR" to color
)
setFragmentResult("Color", bundle)
}
}
}
11 changes: 11 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,23 @@
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.Open_Activity_A).setOnClickListener()
{
startActivity(Intent(this, MainActivity_A::class.java))
}

findViewById<Button>(R.id.Open_Activity_B).setOnClickListener()
{
startActivity(Intent(this, MainActivity_B::class.java))
}
}
}
23 changes: 23 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/MainActivity_A.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package otus.gpb.homework.fragments

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

class MainActivity_A : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_a)
findViewById<Button>(R.id.OpenFragment_A).setOnClickListener()
{
if(savedInstanceState == null)
{
val Fr_A = Fragment_A()
supportFragmentManager.beginTransaction()
.replace(R.id.FrameLayout, Fr_A, "Fr_A")
.addToBackStack(null)
.commit()
}
}
}
}
27 changes: 27 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/MainActivity_B.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 android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity

class MainActivity_B : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_b)
val Fr_BA = Fragment_BA()
if (findViewById<FrameLayout>(R.id.Fr_Port) != null)
{
supportFragmentManager.beginTransaction()
.replace(R.id.Fr_Port, Fr_BA, "Fr_BA")
.commit()
}
else
{
val Fr_BB = Fragment_BB()
supportFragmentManager.beginTransaction()
.replace(R.id.Fr_Fragment_BA, Fr_BA, "Fr_BA")
.replace(R.id.Fr_Fragment_BB, Fr_BB, "Fr_BB")
.commit()
}
}
}
16 changes: 16 additions & 0 deletions app/src/main/res/layout-land/activity_main_b.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<FrameLayout
android:id="@+id/Fr_Fragment_BA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<FrameLayout
android:id="@+id/Fr_Fragment_BB"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
15 changes: 15 additions & 0 deletions app/src/main/res/layout-port/fragment__b_a.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".Fragment_BA">

<!-- TODO: Update blank fragment layout -->

<Button
android:id="@+id/OpenFragment_BB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Fragment BB" />
</FrameLayout>
24 changes: 13 additions & 11 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<?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"
<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=".MainActivity">
android:orientation="vertical">

<TextView
<Button
android:id="@+id/Open_Activity_A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:layout_gravity="center"
android:text="Open Activity A" />

</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="@+id/Open_Activity_B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Open Activity B" />
</LinearLayout>
Loading