Skip to content

Instantly share code, notes, and snippets.

@webserveis

webserveis/MyKotlinExt.kt

Last active Apr 26, 2021
Embed
What would you like to do?
Componente Navigation de Arquitectura de componentes de Google

Dependencias

Añadir en app.gradle las dependencias siguientes

    //Navigation component
    implementation 'androidx.navigation:navigation-fragment-ktx:2.2.0-rc02'
    implementation 'androidx.navigation:navigation-ui-ktx:2.2.0-rc02'

Recursos

Cadenas de texto

    <string name="btn_go_start_title">Empezar</string>
    <string name="btn_say_hello_title">Di Hola</string>
    <string name="welcome_msg">Hola Mundo</string>

Fragmentos

Añadir dos fragmento en blanco y en su layout poner Fragmento A, Fragmento B

Componente Navigation

Añadir nuevo recurso de tipo navigation y poner nombre como nav_graph Añadir el fragmento blanco como principal

En el layout de MainActivity.xml añadir

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />

En MainActivity

private lateinit var navController : NavController
...
val host: NavHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment? ?: return
navController = host.navController

toolbar.setupWithNavController(navController)

Para navegar hacia un fragmento

navController.navigateSafe(R.id.action_fragmentA_to_fragmentB)

Otros

val appBarConfiguration = AppBarConfiguration(navController.graph)
NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration)

https://proandroiddev.com/navigation-architecture-component-for-the-rest-of-us-faafa890e5 https://www.it-swarm.net/es/android/componente-de-la-arquitectura-de-navegacion-de-android-iconos-de-cajon-de-navegacion/805766598/

import android.content.Context
import android.os.Bundle
import android.widget.Toast
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import androidx.navigation.NavController
import androidx.navigation.NavOptions
import androidx.navigation.Navigator
/*
http://kotlinextensions.com/
https://github.com/ravidsrk/kotlinextensions.com
*/
fun Context?.toast(text: CharSequence, duration: Int = Toast.LENGTH_LONG) = this?.let { Toast.makeText(it, text, duration).show() }
fun Context?.toast(@StringRes textId: Int, duration: Int = Toast.LENGTH_LONG) = this?.let { Toast.makeText(it, textId, duration).show() }
fun NavController.navigateSafe(
@IdRes resId: Int,
args: Bundle? = null,
navOptions: NavOptions? = null,
navExtras: Navigator.Extras? = null
) {
val action = currentDestination?.getAction(resId) ?: graph.getAction(resId)
if (action != null && currentDestination?.id != action.destinationId) {
navigate(resId, args, navOptions, navExtras)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment