Navigation

Understanding Navigation Component and its implementation in Android

Introduction to Navigation

Navigation Component Android में app navigation को handle करने का एक modern और efficient way provide करता है। यह single-activity architecture को support करता है और navigation को declarative way में define करने की allow करता है।

Navigation Component provides a modern and efficient way to handle app navigation in Android. It supports single-activity architecture and allows defining navigation in a declarative way.

Key Features (मुख्य विशेषताएं):

  • Navigation Component visual navigation editor provide करता है
  • Navigation Component provides visual navigation editor
  • Navigation Component type-safe argument passing को support करता है
  • Navigation Component supports type-safe argument passing
  • Navigation Component deep linking को support करता है
  • Navigation Component supports deep linking
  • Navigation Component navigation UI patterns को handle करता है
  • Navigation Component handles navigation UI patterns

Navigation Components (नेविगेशन कंपोनेंट्स)

Navigation Component के main components:

Main components of Navigation Component:

Implementation Example (इम्प्लीमेंटेशन उदाहरण)

Navigation Component को implement करने का example:

Example of implementing Navigation Component:

// Add Navigation dependency
dependencies {
    implementation 'androidx.navigation:navigation-fragment-ktx:2.7.7'
    implementation 'androidx.navigation:navigation-ui-ktx:2.7.7'
}

// Navigation Graph (nav_graph.xml)



    
        
    

    
        
    


// Main Activity
class MainActivity : AppCompatActivity() {
    private lateinit var navController: NavController

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

        val navHostFragment = supportFragmentManager
            .findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        navController = navHostFragment.navController

        setupActionBarWithNavController(navController)
    }

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

// Navigation in Fragment
class HomeFragment : Fragment() {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        
        view.findViewById

Deep Linking (डीप लिंकिंग)

Deep linking का example:

Example of deep linking:

// Add deep link to navigation graph

    
    


// Handle deep link in Activity
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val navController = findNavController(R.id.nav_host_fragment)
        navController.handleDeepLink(intent)
    }
}

Best Practices (सर्वोत्तम प्रथाएं)

Navigation Component के साथ काम करने के best practices:

Best practices for working with Navigation Component:

  • Single Activity: Single-activity architecture का use करें
  • Single Activity: Use single-activity architecture
  • Type Safety: Type-safe arguments का use करें
  • Type Safety: Use type-safe arguments
  • Back Stack: Back stack को properly manage करें
  • Back Stack: Properly manage back stack
  • Testing: Navigation flow को test करें
  • Testing: Test navigation flow