-
-
Save c4software/6eb4bfca2d82bc04962b0818f676d8df to your computer and use it in GitHub Desktop.
Fragment + Navigation Graph
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:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".ui.bottom.BottomActivity"> | |
<androidx.fragment.app.FragmentContainerView | |
android:id="@+id/nav_host_fragment" | |
android:name="androidx.navigation.fragment.NavHostFragment" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
app:defaultNavHost="true" | |
app:navGraph="@navigation/mobile_navigation"/> | |
<com.google.android.material.bottomnavigation.BottomNavigationView | |
android:id="@+id/bottom_navigation" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:menu="@menu/bottom_navigation_menu" /> | |
</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
class BottomActivity : AppCompatActivity() { | |
companion object { | |
fun getStartIntent(context: Context): Intent { | |
return Intent(context, BottomActivity::class.java) | |
} | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_bottom) | |
setUpNavigation() | |
} | |
fun setUpNavigation() { | |
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment? | |
NavigationUI.setupWithNavController(bottom_navigation, navHostFragment!!.navController) | |
} | |
} |
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
class Page1 : Fragment() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
} | |
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | |
// Inflate the layout for this fragment | |
return inflater.inflate(R.layout.fragment_page1, container, false) | |
} | |
companion object { | |
fun newInstance() = Page1() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment