Last active
April 16, 2019 14:06
-
-
Save freaksgit/79a9b2cc45ee3b58e0810fcef18b944f to your computer and use it in GitHub Desktop.
Navigation component keep state navigator sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<fragment | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintBottom_toTopOf="@id/bottomNav" | |
android:id="@+id/nav_host_fragment" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:name="androidx.navigation.fragment.NavHostFragment" | |
app:defaultNavHost="true"/> | |
<com.google.android.material.bottomnavigation.BottomNavigationView | |
android:id="@+id/bottomNav" | |
android:layout_width="0dp" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintTop_toBottomOf="@id/nav_host_fragment" | |
app:layout_constraintBottom_toBottomOf="parent" | |
android:layout_height="wrap_content" | |
android:background="?android:attr/windowBackground" | |
app:menu="@menu/menu_navigation"/> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<fragment | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/nav_host_fragment1" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:name="androidx.navigation.fragment.NavHostFragment" | |
app:navGraph="@navigation/navigation1" | |
app:defaultNavHost="true"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<fragment | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/nav_host_fragment2" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:name="androidx.navigation.fragment.NavHostFragment" | |
app:navGraph="@navigation/navigation2" | |
app:defaultNavHost="true"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Bundle | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.navigation.NavController | |
import androidx.navigation.Navigation | |
import androidx.navigation.plusAssign | |
import androidx.navigation.ui.NavigationUI | |
import navigation.KeepStateNavigator | |
import kotlinx.android.synthetic.main.activity_main.* | |
class MainActivity : AppCompatActivity() { | |
private val navController: NavController | |
get() { | |
return Navigation.findNavController(this, R.id.nav_host_fragment) | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
setupNavigation() | |
} | |
private fun setupNavigation() { | |
supportFragmentManager.findFragmentById(R.id.nav_host_fragment)?.let { navHostFragment -> | |
// setup custom navigator | |
val navigator = KeepStateNavigator(this, navHostFragment.childFragmentManager, R.id.nav_host_fragment) | |
navController.navigatorProvider += navigator | |
// set navigation graph | |
navController.setGraph(R.navigation.mobile_navigation) | |
NavigationUI.setupWithNavController(bottomNav, navController) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment