마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 navigationdrawer 에서 햄버거 모양 유지하기

0 추천

 

지금은 저 햄버거 메뉴를 통해 모든노트가 아닌 다른 페이지로 가면 아래와 사진같이 뒤로가기 버튼이 나옵니다

뒤로가기 버튼이 아니라 햄버거를 통해 이동한 페이지에서는 같은 햄버거가 나왔으면 하는데 방법이 있을까요??

 

class MainActivity : AppCompatActivity() , NavigationView.OnNavigationItemSelectedListener, TempToolbarTitleListener{

    lateinit var toolbar : Toolbar
    lateinit var appBarConfiguration : AppBarConfiguration
    lateinit var navController : NavController
    lateinit var navigationView: NavigationView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)


        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        val navController = navHostFragment.navController
        appBarConfiguration = AppBarConfiguration(navController.graph,drawer_layout)

        toolbar = findViewById(R.id.toolbar)
        toolbar.setupWithNavController(navController, appBarConfiguration)
        nav_view.setupWithNavController(navController)
        nav_view.setNavigationItemSelectedListener(this)


    }

    override fun onSupportNavigateUp(): Boolean {
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }

    override fun updateTitle(title: String) {
        toolbar.title = title
    }

    override fun onBackPressed() {
        if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
            drawer_layout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        val navController = findNavController(R.id.nav_host_fragment)
        when(item.itemId){
            R.id.memoListFragment ->{
                Log.d("TAG", "totalmemo click")
                navController.navigate(R.id.memoListFragment)
                drawer_layout.closeDrawer(GravityCompat.START)
            }
            R.id.secretMemoListFragment ->{
                Log.d("TAG","secret Click")
                navController.navigate(R.id.secretMemoListFragment)
                drawer_layout.closeDrawer(GravityCompat.START)
            }
            R.id.addMemoFragment ->{
                Log.d("TAG","trashCan Click")
                navController.navigate(R.id.addMemoFragment)
                drawer_layout.closeDrawer(GravityCompat.START)
            }
        }
        return super.onOptionsItemSelected(item)
    }

}

 

따깡 (420 포인트) 님이 2021년 11월 8일 질문

1개의 답변

+1 추천
 
채택된 답변

 아래 문서를 확인해 보세요. (한글판이 있으니 찾아보시길)

 

https://developer.android.com/guide/navigation/navigation-ui#appbarconfiguration

 

AppBarConfiguration

NavigationUI uses an AppBarConfiguration object to manage the behavior of the Navigation button in the upper-left corner of your app's display area. The Navigation button’s behavior changes depending on whether the user is at a top-level destination.

A top-level destination is the root, or highest level destination, in a set of hierarchically-related destinations. Top-level destinations do not display an Up button in the top app bar because there is no higher level destination. By default, the start destination of your app is the only top-level destination.

When the user is at a top-level destination, the Navigation button becomes a drawer icon image if the destination uses a DrawerLayout. If the destination doesn't use a DrawerLayout, the Navigation button is hidden. When the user is on any other destination, the Navigation button appears as an Up button image. To configure the Navigation button using only the start destination as the top-level destination, create an AppBarConfiguration object, and pass in the corresponding navigation graph, as shown below:

val appBarConfiguration = AppBarConfiguration(navController.graph)

In some cases, you might need to define multiple top-level destinations instead of using the default start destination. Using a BottomNavigationView is a common use case for this, where you may have sibling screens that are not hierarchically related to each other and may each have their own set of related destinations. For cases like these, you can instead pass a set of destination IDs to the constructor, as shown below:

val appBarConfiguration = AppBarConfiguration(setOf(R.id.main, R.id.profile))
spark (227,470 포인트) 님이 2021년 11월 8일 답변
따깡님이 2021년 11월 8일 채택됨
...