This repository has been archived by the owner on Aug 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 273
/
MainActivity.kt
137 lines (117 loc) · 5.61 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.codelabs.navigation
import android.content.res.Resources
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import com.google.android.material.navigation.NavigationView
/**
* A simple activity demonstrating use of a NavHostFragment with a navigation drawer.
*/
class MainActivity : AppCompatActivity() {
private lateinit var appBarConfiguration : AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.navigation_activity)
val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
val host: NavHostFragment = supportFragmentManager
.findFragmentById(R.id.my_nav_host_fragment) as NavHostFragment? ?: return
// Set up Action Bar
val navController = host.navController
appBarConfiguration = AppBarConfiguration(navController.graph)
// TODO STEP 9.5 - Create an AppBarConfiguration with the correct top-level destinations
// You should also remove the old appBarConfiguration setup above
// val drawerLayout : DrawerLayout? = findViewById(R.id.drawer_layout)
// appBarConfiguration = AppBarConfiguration(
// setOf(R.id.home_dest, R.id.deeplink_dest),
// drawerLayout)
// TODO END STEP 9.5
setupActionBar(navController, appBarConfiguration)
setupNavigationMenu(navController)
setupBottomNavMenu(navController)
navController.addOnDestinationChangedListener { _, destination, _ ->
val dest: String = try {
resources.getResourceName(destination.id)
} catch (e: Resources.NotFoundException) {
Integer.toString(destination.id)
}
Toast.makeText(this@MainActivity, "Navigated to $dest",
Toast.LENGTH_SHORT).show()
Log.d("NavigationActivity", "Navigated to $dest")
}
}
private fun setupBottomNavMenu(navController: NavController) {
// TODO STEP 9.3 - Use NavigationUI to set up Bottom Nav
// val bottomNav = findViewById<BottomNavigationView>(R.id.bottom_nav_view)
// bottomNav?.setupWithNavController(navController)
// TODO END STEP 9.3
}
private fun setupNavigationMenu(navController: NavController) {
// TODO STEP 9.4 - Use NavigationUI to set up a Navigation View
// // In split screen mode, you can drag this view out from the left
// // This does NOT modify the actionbar
// val sideNavView = findViewById<NavigationView>(R.id.nav_view)
// sideNavView?.setupWithNavController(navController)
// TODO END STEP 9.4
}
private fun setupActionBar(navController: NavController,
appBarConfig : AppBarConfiguration) {
// TODO STEP 9.6 - Have NavigationUI handle what your ActionBar displays
// // This allows NavigationUI to decide what label to show in the action bar
// // By using appBarConfig, it will also determine whether to
// // show the up arrow or drawer menu icon
// setupActionBarWithNavController(navController, appBarConfig)
// TODO END STEP 9.6
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val retValue = super.onCreateOptionsMenu(menu)
val navigationView = findViewById<NavigationView>(R.id.nav_view)
// The NavigationView already has these same navigation items, so we only add
// navigation items to the menu here if there isn't a NavigationView
if (navigationView == null) {
menuInflater.inflate(R.menu.overflow_menu, menu)
return true
}
return retValue
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return super.onOptionsItemSelected(item)
// TODO STEP 9.2 - Have Navigation UI Handle the item selection - make sure to delete
// the old return statement above
// // Have the NavigationUI look for an action or destination matching the menu
// // item id and navigate there if found.
// // Otherwise, bubble up to the parent.
// return item.onNavDestinationSelected(findNavController(R.id.my_nav_host_fragment))
// || super.onOptionsItemSelected(item)
// TODO END STEP 9.2
}
// TODO STEP 9.7 - Have NavigationUI handle up behavior in the ActionBar
// override fun onSupportNavigateUp(): Boolean {
// // Allows NavigationUI to support proper up navigation or the drawer layout
// // drawer menu, depending on the situation
// return findNavController(R.id.my_nav_host_fragment).navigateUp(appBarConfiguration)
// }
// TODO END STEP 9.7
}