Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Main activity layout:
- <androidx.fragment.app.FragmentContainerView
- android:id="@+id/nav_host_fragment_container"
- 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=".ui.main.MainActivity"
- android:name="androidx.navigation.fragment.NavHostFragment"
- app:defaultNavHost="true"
- app:navGraph="@navigation/nav_graph" />
- Nav_grav:
- <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/nav_graph"
- app:startDestination="@id/loginFragment">
- <fragment
- android:id="@+id/loginFragment"
- android:name="com.retroroots.vila.ui.login.LoginFragment"
- android:label="fragment_login"
- tools:layout="@layout/fragment_login" >
- <action
- android:id="@+id/action_loginFragment_to_homeContainerFragment"
- app:destination="@id/homeContainerFragment"
- app:popUpTo="@id/loginFragment"
- app:popUpToInclusive="true"/>
- </fragment>
- <fragment
- android:id="@+id/homeContainerFragment"
- android:name="com.retroroots.vila.ui.Home.HomeContainerFragment"
- android:label="fragment_home_container"
- tools:layout="@layout/fragment_home_container" />
- </navigation>
- Login fragment layout:
- <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"
- android:background="@drawable/app_background"
- android:paddingTop="100dp"
- tools:context=".ui.login.LoginFragment"
- android:animateLayoutChanges="true">
- <com.google.android.material.button.MaterialButton
- android:id="@+id/loginBtn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- </androidx.constraintlayout.widget.ConstraintLayout>
- The above button runs this from the login fragment:
- NavDirections navDirections = LoginFragmentDirections.actionLoginFragmentToHomeContainerFragment();
- NavHostFragment.findNavController(currentFragment).navigate(navDirections);
- Home Fragment layout:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- tools:context=".ui.Home.HomeContainerFragment"
- android:orientation="vertical"
- android:id="@+id/homeLayout">
- <com.google.android.material.bottomnavigation.BottomNavigationView
- android:id="@+id/bottomNavVw"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:menu="@menu/home_top_nav_menu" />
- <androidx.fragment.app.FragmentContainerView
- android:id="@+id/homeNavHostFragmentContainer"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:name="androidx.navigation.fragment.NavHostFragment"
- android:padding="10dp"
- app:navGraph="@navigation/home_nav_graph"/>
- </LinearLayout>
- This runs from the home's fragment "onViewCreated":
- BottomNavigationView bottomNavigationView = view.findViewById(R.id.bottomNavVw);
- NavController navController = NavHostFragment.findNavController(this);
- NavigationUI.setupWithNavController(bottomNavigationView, navController);
- Menu home:
- <menu xmlns:android="http://schemas.android.com/apk/res/android">
- <item
- android:id="@+id/pendingOrdersFragment"
- android:title="@string/pendingOrdersTabTxt" />
- <item
- android:id="@+id/historyFragment"
- android:title="@string/historyTabTxt" />
- </menu>
- pendingOrdersFragment & historyFragment layout: Just a single text view with the name of the fragment
- Home nav graph:
- <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/home_nav_graph"
- app:startDestination="@id/pendingOrdersFragment">
- <fragment
- android:id="@+id/pendingOrdersFragment"
- android:name="com.retroroots.vila.ui.Home.pending.PendingOrdersFragment"
- android:label="fragment_pending_orders"
- tools:layout="@layout/fragment_pending_orders" />
- <fragment
- android:id="@+id/historyFragment"
- android:name="com.retroroots.vila.ui.Home.history.HistoryFragment"
- android:label="fragment_history"
- tools:layout="@layout/fragment_history" />
- </navigation>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement