I made a working navigation drawer like it\'s shown in the tutorial on the developer.android.com website. But now, I want to use one Navigation Drawer, i created in the Navi
I do it in Kotlin like this:
open class BaseAppCompatActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
protected lateinit var drawerLayout: DrawerLayout
protected lateinit var navigationView: NavigationView
@Inject
lateinit var loginService: LoginService
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d("BaseAppCompatActivity", "onCreate()")
App.getComponent().inject(this)
drawerLayout = findViewById(R.id.drawer_layout) as DrawerLayout
val toolbar = findViewById(R.id.toolbar) as Toolbar
setSupportActionBar(toolbar)
navigationView = findViewById(R.id.nav_view) as NavigationView
navigationView.setNavigationItemSelectedListener(this)
val toggle = ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
toggle.isDrawerIndicatorEnabled = true
val navigationViewHeaderView = navigationView.getHeaderView(0)
navigationViewHeaderView.login_txt.text = SharedKey.username
}
private inline fun launch():Boolean{
if(this is T) return closeDrawer()
val intent = Intent(applicationContext, T::class.java)
startActivity(intent)
finish()
return true
}
private fun closeDrawer(): Boolean {
drawerLayout.closeDrawer(GravityCompat.START)
return true
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
val id = item.itemId
when (id) {
R.id.action_tasks -> {
return launch()
}
R.id.action_contacts -> {
return launch()
}
R.id.action_logout -> {
createExitDialog(loginService, this)
}
}
return false
}
}
Activities for drawer must inherit this BaseAppCompatActivity
, call super.onCreate
after content is set (actually, can be moved to some init method) and have corresponding elements for ids in their layout