Skip to content

Commit 88ba09c

Browse files
author
elgroup
committed
commit by aryan on 2-08-2019
1 parent a23bced commit 88ba09c

23 files changed

+373
-65
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ dependencies {
2929
implementation 'androidx.appcompat:appcompat:1.0.2'
3030
implementation 'androidx.core:core-ktx:1.0.2'
3131
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
32+
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
3233
testImplementation 'junit:junit:4.12'
3334
androidTestImplementation 'androidx.test:runner:1.2.0'
3435
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

app/src/main/java/com/example/backstack_bottomnavigation/MainActivity.kt

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.view.MenuItem
88
import android.widget.Toast
99
import androidx.annotation.RequiresApi
1010
import androidx.fragment.app.Fragment
11+
import androidx.fragment.app.FragmentManager
1112
import com.example.backstack_bottomnavigation.R
1213
import com.example.backstack_bottomnavigation.fragments.HomeFragment
1314
import com.example.backstack_bottomnavigation.fragments.MidFragment
@@ -116,37 +117,60 @@ return false
116117
}
117118

118119
override fun onBackPressed() {
119-
var toptag = FragmentStack.getInstance(3).pop(currentTag)
120-
if (toptag!=null){
121-
var topfrag= supportFragmentManager.findFragmentByTag(toptag)
122-
supportFragmentManager.beginTransaction().hide(currentFragment!!).show(topfrag!!).commit()
123-
currentTag=toptag
124-
currentFragment=topfrag
125-
if (currentTag.equals("home")){
126-
bottomNavigation.selectedItemId=R.id.action_home
127-
}else if (currentTag.equals("mid")){
128-
129-
bottomNavigation.selectedItemId=R.id.action_mid
130-
}else if (currentTag.equals("profile")){
131-
132-
bottomNavigation.selectedItemId=R.id.action_profile
133-
}
120+
121+
var currentFrag=supportFragmentManager.findFragmentByTag(currentTag)
122+
123+
var childfragmanager: FragmentManager?=null
124+
if (currentFrag is ProfileFragment){
125+
childfragmanager=currentFrag.getchildFragmentManager()
126+
}else if (currentFrag is MidFragment){
127+
128+
childfragmanager=currentFrag.getchildFragmentManager()
129+
}else if (currentFrag is HomeFragment){
130+
131+
childfragmanager=currentFrag.getchildFragmentManager()
132+
}
133+
if (childfragmanager?.backStackEntryCount!!>0){
134+
childfragmanager.popBackStackImmediate()
135+
}
136+
else{
137+
138+
var toptag = FragmentStack.getInstance(3).pop(currentTag)
139+
if (toptag!=null){
140+
var topfrag= supportFragmentManager.findFragmentByTag(toptag)
141+
supportFragmentManager.beginTransaction().hide(currentFragment!!).show(topfrag!!).commit()
142+
currentTag=toptag
143+
currentFragment=topfrag
144+
if (currentTag.equals("home")){
145+
bottomNavigation.selectedItemId=R.id.action_home
146+
}else if (currentTag.equals("mid")){
147+
148+
bottomNavigation.selectedItemId=R.id.action_mid
149+
}else if (currentTag.equals("profile")){
150+
151+
bottomNavigation.selectedItemId=R.id.action_profile
152+
}
134153

135154

136-
}else{
137-
if (currentTag.equals("home")){
138-
super.onBackPressed()
139155
}else{
156+
if (currentTag.equals("home")){
157+
super.onBackPressed()
158+
}else{
159+
160+
var newfrag= supportFragmentManager.findFragmentByTag("home")
161+
supportFragmentManager.beginTransaction().hide(currentFragment!!).show(newfrag!!).commit()
162+
currentTag="home"
163+
currentFragment=newfrag
140164

141-
var newfrag= supportFragmentManager.findFragmentByTag("home")
142-
supportFragmentManager.beginTransaction().hide(currentFragment!!).show(newfrag!!).commit()
143-
currentTag="home"
144-
currentFragment=newfrag
165+
bottomNavigation.selectedItemId=R.id.action_home
166+
}
145167

146-
bottomNavigation.selectedItemId=R.id.action_home
147168
}
148169

149170
}
150171

172+
173+
174+
151175
}
152176
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.example.backstack_bottomnavigation.fragments
2+
3+
4+
import android.os.Bundle
5+
import androidx.fragment.app.Fragment
6+
import android.view.LayoutInflater
7+
import android.view.View
8+
import android.view.ViewGroup
9+
10+
import com.example.backstack_bottomnavigation.R
11+
12+
// TODO: Rename parameter arguments, choose names that match
13+
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
14+
private const val ARG_PARAM1 = "param1"
15+
private const val ARG_PARAM2 = "param2"
16+
17+
/**
18+
* A simple [Fragment] subclass.
19+
*
20+
*/
21+
class AboutmeFragment : Fragment() {
22+
23+
override fun onCreateView(
24+
inflater: LayoutInflater, container: ViewGroup?,
25+
savedInstanceState: Bundle?
26+
): View? {
27+
// Inflate the layout for this fragment
28+
return inflater.inflate(R.layout.fragment_aboutme, container, false)
29+
}
30+
31+
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.example.backstack_bottomnavigation.fragments
2+
3+
4+
import android.os.Bundle
5+
import androidx.fragment.app.Fragment
6+
import android.view.LayoutInflater
7+
import android.view.View
8+
import android.view.ViewGroup
9+
10+
import com.example.backstack_bottomnavigation.R
11+
12+
// TODO: Rename parameter arguments, choose names that match
13+
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
14+
private const val ARG_PARAM1 = "param1"
15+
private const val ARG_PARAM2 = "param2"
16+
17+
/**
18+
* A simple [Fragment] subclass.
19+
*
20+
*/
21+
class DetailFragment : Fragment() {
22+
23+
override fun onCreateView(
24+
inflater: LayoutInflater, container: ViewGroup?,
25+
savedInstanceState: Bundle?
26+
): View? {
27+
// Inflate the layout for this fragment
28+
return inflater.inflate(R.layout.fragment_detail, container, false)
29+
}
30+
31+
32+
}

app/src/main/java/com/example/backstack_bottomnavigation/fragments/HomeFragment.kt

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import androidx.fragment.app.Fragment
77
import android.view.LayoutInflater
88
import android.view.View
99
import android.view.ViewGroup
10+
import android.widget.TextView
11+
import androidx.fragment.app.FragmentManager
1012
import com.example.backstack_bottomnavigation.R
1113

1214

@@ -20,13 +22,33 @@ private const val ARG_PARAM2 = "param2"
2022
*
2123
*/
2224
class HomeFragment : Fragment() {
23-
25+
lateinit var about_me:TextView
26+
lateinit var mrootView: View
2427
override fun onCreateView(
2528
inflater: LayoutInflater, container: ViewGroup?,
2629
savedInstanceState: Bundle?
2730
): View? {
2831
// Inflate the layout for this fragment doWork()
29-
return inflater.inflate(R.layout.fragment_home, container, false)
32+
mrootView= inflater.inflate(R.layout.fragment_home, container, false)
33+
init()
34+
return mrootView
35+
}
36+
37+
private fun init() {
38+
39+
about_me=mrootView.findViewById(R.id.about_me)
40+
about_me.setOnClickListener {
41+
about_me()
42+
}
43+
}
44+
45+
fun about_me(){
46+
childFragmentManager.beginTransaction().add(R.id.home_container,AboutmeFragment()).addToBackStack(null).commit()
47+
}
48+
49+
fun getchildFragmentManager(): FragmentManager {
50+
return childFragmentManager
51+
3052
}
3153

3254
}

app/src/main/java/com/example/backstack_bottomnavigation/fragments/MidFragment.kt

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import androidx.fragment.app.Fragment
66
import android.view.LayoutInflater
77
import android.view.View
88
import android.view.ViewGroup
9+
import android.widget.TextView
10+
import androidx.fragment.app.FragmentManager
911
import com.example.backstack_bottomnavigation.R
1012

1113

@@ -19,13 +21,34 @@ private const val ARG_PARAM2 = "param2"
1921
*
2022
*/
2123
class MidFragment : Fragment() {
22-
24+
lateinit var detail:TextView
25+
lateinit var mrootview:View
2326
override fun onCreateView(
2427
inflater: LayoutInflater, container: ViewGroup?,
2528
savedInstanceState: Bundle?
2629
): View? {
2730
// Inflate the layout for this fragment
28-
return inflater.inflate(R.layout.fragment_mid, container, false)
31+
mrootview= inflater.inflate(R.layout.fragment_mid, container, false)
32+
init()
33+
return mrootview
34+
35+
}
36+
37+
private fun init() {
38+
39+
detail= mrootview.findViewById(R.id.detail)
40+
detail.setOnClickListener {
41+
detail()
42+
}
43+
}
44+
45+
fun detail(){
46+
childFragmentManager.beginTransaction().add(R.id.mid_container,DetailFragment()).addToBackStack(null).commit()
47+
}
48+
49+
fun getchildFragmentManager(): FragmentManager {
50+
return childFragmentManager
51+
2952
}
3053

3154

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.example.backstack_bottomnavigation.fragments
2+
3+
4+
import android.os.Bundle
5+
import androidx.fragment.app.Fragment
6+
import android.view.LayoutInflater
7+
import android.view.View
8+
import android.view.ViewGroup
9+
10+
import com.example.backstack_bottomnavigation.R
11+
12+
// TODO: Rename parameter arguments, choose names that match
13+
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
14+
private const val ARG_PARAM1 = "param1"
15+
private const val ARG_PARAM2 = "param2"
16+
17+
/**
18+
* A simple [Fragment] subclass.
19+
*
20+
*/
21+
class MyInfoFragment : Fragment() {
22+
23+
override fun onCreateView(
24+
inflater: LayoutInflater, container: ViewGroup?,
25+
savedInstanceState: Bundle?
26+
): View? {
27+
// Inflate the layout for this fragment
28+
return inflater.inflate(R.layout.fragment_my_info, container, false)
29+
}
30+
31+
32+
}

app/src/main/java/com/example/backstack_bottomnavigation/fragments/ProfileFragment.kt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import androidx.fragment.app.Fragment
66
import android.view.LayoutInflater
77
import android.view.View
88
import android.view.ViewGroup
9+
import android.widget.LinearLayout
10+
import android.widget.TextView
11+
import androidx.fragment.app.FragmentManager
12+
import com.example.backstack_bottomnavigation.MainActivity
913
import com.example.backstack_bottomnavigation.R
1014

1115

@@ -20,13 +24,35 @@ private const val ARG_PARAM2 = "param2"
2024
*/
2125
class ProfileFragment : Fragment() {
2226

27+
lateinit var more_about_me:TextView
28+
lateinit var mrootView: View
2329
override fun onCreateView(
2430
inflater: LayoutInflater, container: ViewGroup?,
2531
savedInstanceState: Bundle?
2632
): View? {
2733
// Inflate the layout for this fragment
28-
return inflater.inflate(R.layout.fragment_profile, container, false)
34+
mrootView= inflater.inflate(R.layout.fragment_profile, container, false)
35+
init()
36+
return mrootView
2937
}
3038

39+
private fun init() {
40+
41+
more_about_me=mrootView.findViewById(R.id.more_about_me)
42+
more_about_me.setOnClickListener {
43+
more_about_me()
44+
}
45+
}
46+
47+
fun more_about_me(){
48+
49+
childFragmentManager.beginTransaction().add(R.id.profile_conainer,MyInfoFragment()).addToBackStack(null).commit()
50+
51+
}
52+
53+
fun getchildFragmentManager(): FragmentManager {
54+
return childFragmentManager
55+
56+
}
3157

3258
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:shape="rectangle">
4+
<corners android:radius="15dp" />
5+
<padding
6+
android:left="15dp"
7+
android:top="4dp"
8+
android:right="15dp"
9+
android:bottom="4dp" />
10+
<stroke
11+
android:width="1dip"
12+
android:color="@color/orange" />
13+
</shape>
1.17 KB
Loading

0 commit comments

Comments
 (0)