Android Cheat Sheet
ViewBinding
build.gradle
1
2
3
4
5
android {
buildFeatures {
viewBinding true
}
}
Activity.java
1
setContentView((binding = ActivityMainBinding.inflate(getLayoutInflater())).getRoot());
Fragment.java
1
return (binding = FragmentMainBinding.inflate(inflater, container, false)).getRoot();
Navigation
1
2
3
4
5
6
7
<androidx.fragment.app.FragmentContainerView
android:name="androidx.navigation.fragment.NavHostFragment"
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
NavController (Fragment)
1
navController = Navigation.findNavController(view);
NavController (Activity)
1
navController = ((NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment)).getNavController();
NavController (DialogFragment)
1
navController = Navigation.findNavController(requireParentView());