Performance tips for Navigation component in Android?

Improve your Android app's performance using the Navigation component with these effective tips!
Android Performance, Navigation Component, Android Optimization, Mobile App Performance, Android Development

// Example: Efficient use of Navigation Component with fragment caching
class NavigationActivity : AppCompatActivity() {
    private lateinit var navController: NavController

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_navigation)

        navController = findNavController(R.id.nav_host_fragment)

        // Optimize by using a single shared ViewModel for multiple fragments.
        val sharedViewModel = ViewModelProvider(this).get(SharedViewModel::class.java)

        // Setup the navigation with the controller
        setupActionBarWithNavController(navController)
    }

    override fun onSupportNavigateUp(): Boolean {
        // Handle the up navigation correctly
        return navController.navigateUp() || super.onSupportNavigateUp()
    }
}
    

Android Performance Navigation Component Android Optimization Mobile App Performance Android Development