Skip to content

Instantly share code, notes, and snippets.

@Zhuinden
Last active June 9, 2020 20:24
Show Gist options
  • Save Zhuinden/6b5ce666a2d900c816f642001cb00a7e to your computer and use it in GitHub Desktop.
Save Zhuinden/6b5ce666a2d900c816f642001cb00a7e to your computer and use it in GitHub Desktop.
Single-Activity: Create Login Credentials (Jetpack Navigation)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation"/>
</FrameLayout>
class CreateLoginCredentialsFragment : Fragment(R.layout.create_login_credentials_fragment) {
private val viewModel by navGraphSavedStateViewModels(R.id.registration_graph) { handle -> // ViewModelUtils.kt
Injector.get().registrationViewModelFactory().create(handle) // Dagger + AssistedInject
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = CreateLoginCredentialsFragmentBinding.bind(view)
// ...
class RegistrationViewModel @AssistedInject constructor(
private val authenticationManager: AuthenticationManager,
@Assisted private val savedStateHandle: SavedStateHandle
) : ViewModel() {
@AssistedInject.Factory
interface Factory {
fun create(savedStateHandle: SavedStateHandle): RegistrationViewModel
}
// ...
}
<?xml version="1.0" encoding="utf-8"?>
<navigation 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"
android:id="@+id/navigation"
app:startDestination="@id/splash_graph"
tools:ignore="UnusedNavigation">
<!-- ... -->
<navigation
android:id="@+id/registration_graph"
app:startDestination="@id/enter_profile_data_fragment">
<action
android:id="@+id/registration_to_logged_in"
app:destination="@id/logged_in_graph"
app:enterAnim="@anim/fragment_fade_enter"
app:exitAnim="@anim/fragment_fade_exit"
app:popEnterAnim="@anim/fragment_fade_enter"
app:popExitAnim="@anim/fragment_fade_exit"
app:popUpTo="@id/logged_out_graph"
app:popUpToInclusive="true">
<argument
android:name="username"
app:argType="string"
app:nullable="false" />
</action>
<fragment
android:id="@+id/create_login_credentials_fragment"
android:name=".features.registration.CreateLoginCredentialsFragment"
tools:layout="@layout/create_login_credentials_fragment" />
</navigation>
</navigation>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment