From bb9b12ebdf5821d64fd3c406cdfd42a073e8bbc0 Mon Sep 17 00:00:00 2001 From: "d.boldyrev" Date: Mon, 7 Jul 2025 10:24:21 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=94=D0=97=20Fragments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 2 + app/src/main/AndroidManifest.xml | 8 +++ .../otus/gpb/homework/fragments/Activity1.kt | 28 ++++++++++ .../otus/gpb/homework/fragments/Activity2.kt | 40 +++++++++++++ .../otus/gpb/homework/fragments/FragmentA.kt | 47 ++++++++++++++++ .../otus/gpb/homework/fragments/FragmentAA.kt | 56 +++++++++++++++++++ .../otus/gpb/homework/fragments/FragmentAB.kt | 47 ++++++++++++++++ .../otus/gpb/homework/fragments/FragmentBA.kt | 56 +++++++++++++++++++ .../otus/gpb/homework/fragments/FragmentBB.kt | 43 ++++++++++++++ .../gpb/homework/fragments/MainActivity.kt | 8 +++ .../res/drawable/baseline_arrow_back_24.xml | 5 ++ app/src/main/res/layout/activity_1.xml | 17 ++++++ app/src/main/res/layout/activity_2.xml | 28 ++++++++++ app/src/main/res/layout/activity_main.xml | 14 ++++- app/src/main/res/layout/fragment_a.xml | 52 +++++++++++++++++ app/src/main/res/layout/fragment_a_a.xml | 33 +++++++++++ app/src/main/res/layout/fragment_a_b.xml | 24 ++++++++ app/src/main/res/layout/fragment_b_a.xml | 34 +++++++++++ app/src/main/res/layout/fragment_b_b.xml | 33 +++++++++++ app/src/main/res/values/strings.xml | 2 + app/src/main/res/values/themes.xml | 2 +- 21 files changed, 576 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/otus/gpb/homework/fragments/Activity1.kt create mode 100644 app/src/main/java/otus/gpb/homework/fragments/Activity2.kt create mode 100644 app/src/main/java/otus/gpb/homework/fragments/FragmentA.kt create mode 100644 app/src/main/java/otus/gpb/homework/fragments/FragmentAA.kt create mode 100644 app/src/main/java/otus/gpb/homework/fragments/FragmentAB.kt create mode 100644 app/src/main/java/otus/gpb/homework/fragments/FragmentBA.kt create mode 100644 app/src/main/java/otus/gpb/homework/fragments/FragmentBB.kt create mode 100644 app/src/main/res/drawable/baseline_arrow_back_24.xml create mode 100644 app/src/main/res/layout/activity_1.xml create mode 100644 app/src/main/res/layout/activity_2.xml create mode 100644 app/src/main/res/layout/fragment_a.xml create mode 100644 app/src/main/res/layout/fragment_a_a.xml create mode 100644 app/src/main/res/layout/fragment_a_b.xml create mode 100644 app/src/main/res/layout/fragment_b_a.xml create mode 100644 app/src/main/res/layout/fragment_b_b.xml diff --git a/app/build.gradle b/app/build.gradle index c5cf1b8..87b742e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9604b34..82cf206 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,6 +12,14 @@ android:supportsRtl="true" android:theme="@style/Theme.Fragments" tools:targetApi="31"> + + diff --git a/app/src/main/java/otus/gpb/homework/fragments/Activity1.kt b/app/src/main/java/otus/gpb/homework/fragments/Activity1.kt new file mode 100644 index 0000000..a2020b1 --- /dev/null +++ b/app/src/main/java/otus/gpb/homework/fragments/Activity1.kt @@ -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() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/otus/gpb/homework/fragments/Activity2.kt b/app/src/main/java/otus/gpb/homework/fragments/Activity2.kt new file mode 100644 index 0000000..09fd7f2 --- /dev/null +++ b/app/src/main/java/otus/gpb/homework/fragments/Activity2.kt @@ -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(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() + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/otus/gpb/homework/fragments/FragmentA.kt b/app/src/main/java/otus/gpb/homework/fragments/FragmentA.kt new file mode 100644 index 0000000..4c25846 --- /dev/null +++ b/app/src/main/java/otus/gpb/homework/fragments/FragmentA.kt @@ -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