Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit 7187f86

Browse files
author
Murat Yener
authored
Merge pull request #993 from android/update-navigation-to-2.4.0alpha01
updates navigation to 2.4.0-alpha01
2 parents 8f4936b + a9ce117 commit 7187f86

File tree

6 files changed

+50
-304
lines changed

6 files changed

+50
-304
lines changed

NavigationAdvancedSample/app/src/androidTest/java/com/example/android/navigationadvancedsample/BottomNavigationTest.kt

-18
Original file line numberDiff line numberDiff line change
@@ -112,24 +112,6 @@ class BottomNavigationTest {
112112
assertDeeperThirdScreen()
113113
}
114114

115-
@Test
116-
fun bottomNavView_itemReselected_goesBackToStart() {
117-
openThirdScreen()
118-
119-
assertThirdScreen()
120-
121-
onView(withContentDescription(R.string.sign_up))
122-
.perform(click())
123-
124-
assertDeeperThirdScreen()
125-
126-
// Reselect the current item
127-
openThirdScreen()
128-
129-
// Verify that it popped the back stack until the start destination.
130-
assertThirdScreen()
131-
}
132-
133115
private fun assertSecondScreen() {
134116
onView(allOf(withText(R.string.title_list), isDescendantOfA(withId(R.id.action_bar))))
135117
.check(matches(isDisplayed()))

NavigationAdvancedSample/app/src/main/java/com/example/android/navigationadvancedsample/MainActivity.kt

+17-33
Original file line numberDiff line numberDiff line change
@@ -18,59 +18,43 @@ package com.example.android.navigationadvancedsample
1818

1919
import android.os.Bundle
2020
import androidx.appcompat.app.AppCompatActivity
21-
import androidx.lifecycle.LiveData
22-
import androidx.lifecycle.Observer
2321
import androidx.navigation.NavController
22+
import androidx.navigation.fragment.NavHostFragment
23+
import androidx.navigation.ui.AppBarConfiguration
24+
import androidx.navigation.ui.navigateUp
2425
import androidx.navigation.ui.setupActionBarWithNavController
26+
import androidx.navigation.ui.setupWithNavController
2527
import com.google.android.material.bottomnavigation.BottomNavigationView
2628

2729
/**
2830
* An activity that inflates a layout that has a [BottomNavigationView].
2931
*/
3032
class MainActivity : AppCompatActivity() {
3133

32-
private var currentNavController: LiveData<NavController>? = null
34+
private lateinit var navController: NavController
35+
private lateinit var appBarConfiguration: AppBarConfiguration
3336

3437
override fun onCreate(savedInstanceState: Bundle?) {
3538
super.onCreate(savedInstanceState)
3639
setContentView(R.layout.activity_main)
37-
if (savedInstanceState == null) {
38-
setupBottomNavigationBar()
39-
} // Else, need to wait for onRestoreInstanceState
40-
}
4140

42-
override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
43-
super.onRestoreInstanceState(savedInstanceState)
44-
// Now that BottomNavigationBar has restored its instance state
45-
// and its selectedItemId, we can proceed with setting up the
46-
// BottomNavigationBar with Navigation
47-
setupBottomNavigationBar()
48-
}
41+
val navHostFragment = supportFragmentManager.findFragmentById(
42+
R.id.nav_host_container
43+
) as NavHostFragment
44+
navController = navHostFragment.navController
4945

50-
/**
51-
* Called on first creation and when restoring state.
52-
*/
53-
private fun setupBottomNavigationBar() {
46+
// Setup the bottom navigation view with navController
5447
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_nav)
48+
bottomNavigationView.setupWithNavController(navController)
5549

56-
val navGraphIds = listOf(R.navigation.home, R.navigation.list, R.navigation.form)
57-
58-
// Setup the bottom navigation view with a list of navigation graphs
59-
val controller = bottomNavigationView.setupWithNavController(
60-
navGraphIds = navGraphIds,
61-
fragmentManager = supportFragmentManager,
62-
containerId = R.id.nav_host_container,
63-
intent = intent
50+
// Setup the ActionBar with navController and 3 top level destinations
51+
appBarConfiguration = AppBarConfiguration(
52+
setOf(R.id.titleScreen, R.id.leaderboard, R.id.register)
6453
)
65-
66-
// Whenever the selected controller changes, setup the action bar.
67-
controller.observe(this, Observer { navController ->
68-
setupActionBarWithNavController(navController)
69-
})
70-
currentNavController = controller
54+
setupActionBarWithNavController(navController, appBarConfiguration)
7155
}
7256

7357
override fun onSupportNavigateUp(): Boolean {
74-
return currentNavController?.value?.navigateUp() ?: false
58+
return navController.navigateUp(appBarConfiguration)
7559
}
7660
}

NavigationAdvancedSample/app/src/main/java/com/example/android/navigationadvancedsample/NavigationExtensions.kt

-250
This file was deleted.

NavigationAdvancedSample/app/src/main/res/layout/activity_main.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424

2525
<androidx.fragment.app.FragmentContainerView
2626
android:id="@+id/nav_host_container"
27+
android:name="androidx.navigation.fragment.NavHostFragment"
2728
android:layout_width="match_parent"
2829
android:layout_height="0dp"
29-
android:layout_weight="1" />
30+
android:layout_weight="1"
31+
app:defaultNavHost="true"
32+
app:navGraph="@navigation/nav_graph" />
3033

3134
<com.google.android.material.bottomnavigation.BottomNavigationView
3235
android:id="@+id/bottom_nav"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright 2019, The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<navigation
18+
xmlns:android="http://schemas.android.com/apk/res/android"
19+
xmlns:app="http://schemas.android.com/apk/res-auto"
20+
android:id="@+id/nav_graph"
21+
app:startDestination="@+id/home">
22+
23+
<include app:graph="@navigation/home"/>
24+
<include app:graph="@navigation/list"/>
25+
<include app:graph="@navigation/form"/>
26+
27+
</navigation>

0 commit comments

Comments
 (0)