-
Notifications
You must be signed in to change notification settings - Fork 68
Fragment #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fragment #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. тут можно проверить сколько фрагментов остался во фрагмент менеджере как описано в пункте 4 задачи |
||
| } | ||
| requireActivity().onBackPressedDispatcher.addCallback( | ||
| this, | ||
| callback | ||
| ) | ||
| } | ||
|
|
||
| } | ||
| 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() | ||
| } | ||
| } | ||
| } |
| 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) | ||
| } | ||
|
|
||
| } |
| 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() | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
| 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) | ||
| } | ||
| } | ||
| } |
| 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)) | ||
| } | ||
| } | ||
| } |
| 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() | ||
| } | ||
| } | ||
| } | ||
| } |
| 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() | ||
| } | ||
| } | ||
| } |
| 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> |
| 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> |
| 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> |
There was a problem hiding this comment.
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