3

I build an app which has a login screen and the home screen. I am following the single activity design principle. I have implemented the Navigation drawer like this. This is my MainActivity

class MainActivity : AppCompatActivity() {

private lateinit var drawerLayout: DrawerLayout
private lateinit var appBarConfiguration: AppBarConfiguration

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

    val binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    drawerLayout = binding.drawerLayout

    val navController = this.findNavController(R.id.navHostFragment)
    NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)

    appBarConfiguration = AppBarConfiguration(navController.graph, drawerLayout)
    NavigationUI.setupWithNavController(binding.navView, navController)
}

override fun onSupportNavigateUp(): Boolean {
    val navController = this.findNavController(R.id.navHostFragment)
    return NavigationUI.navigateUp(navController,drawerLayout)
}
}

MainAcivity layout

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <fragment
            android:id="@+id/navHostFragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/navigation" />
    </FrameLayout>
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/navdrawer" />
</androidx.drawerlayout.widget.DrawerLayout>

I also have two fragments loginFrament and homeFragment now the navigation drawer is shown both of this fragment but I want it to be in the Home Screen fragment only. checked some resources how to hide navigation drawer. but this method only lock the drawer.

2
  • How do you navigate to loginFragment? Sep 11, 2020 at 7:43
  • I am navigating using the navigation Graph, I am using the navigation design pattern from jetpacks @GabrieleMariotti
    – Dharmaraj
    Sep 11, 2020 at 11:35

1 Answer 1

0

I've same design app and i made login/register screens with activity and all other screens which i want to navigte with nav drawer made with fragments, also in some submenus which i don't want to show drawer, i used activity again. Activity & fragments works pretty good together. Navigating without drawer between activities looks more easier to me tbh. Happy codin!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.