飞溅后,我的应用程序进入一个主活动,其中导航按钮有 4 个选项卡,但在用户登录之前,他应该看到登录片段而不是个人资料选项卡,但登录后,他可以看到个人资料选项卡。 我保存令牌并使用布尔值检查它是否已登录,但这不是真的有人可以帮忙吗 我使用带有底部导航的导航组件。 可以在菜单中写入 switch case 并检查我的布尔值吗?
<?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_graf"
app:startDestination="@+id/menu_store">
<fragment
android:id="@+id/menu_store"
android:name="com.ostovane.android.view.fragments.BaseStoreFragment"
android:label="@string/base_store"
tools:layout="@layout/fragment_base_store" />
<fragment
android:id="@+id/menu_search"
android:name="com.ostovane.android.view.fragments.SearchFragment"
android:label="@string/search"
tools:layout="@layout/fragment_search" />
<fragment
android:id="@+id/menu_category"
android:name="com.ostovane.android.view.fragments.CategoryFragment"
android:label="@string/category"
tools:layout="@layout/fragment_category" />
<fragment
android:id="@+id/menu_my_products"
android:name="com.ostovane.android.view.fragments.MyProductsFragment"
android:label="@string/my_products"
tools:layout="@layout/fragment_myproduct" />
<fragment
android:id="@+id/menu_profile"
android:name="com.ostovane.android.view.fragments.ProfileFragment"
android:label="@string/profile"
tools:layout="@layout/fragment_profile">
<action
android:id="@+id/action_profile_dest_to_login_dest"
app:destination="@id/authenticate"
app:popUpTo="@+id/profile_dest"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/authenticate"
android:name="com.ostovane.android.view.fragments.auth.UnAuthenticatedFragment"
android:label="@string/authenticate"
tools:layout="@layout/fragment_un_authenticated" />
</navigation>
一些主要活动
导入 dagger.hilt.android.AndroidEntryPoint;
@AndroidEntryPoint 公共类 MainActivity 扩展了 BaseActivity {
private ActivityMainBinding binding;
private boolean isLogin = false;
private String authToken;
@Inject
ProfileFragment profileFragment;
@Inject
UnAuthenticatedFragment authenticatedFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
BottomNavigationView bottomNavigationView = binding.bottomNavigationBar;
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
NavInflater navInflater = navController.getNavInflater();
NavGraph graph = navInflater.inflate(R.navigation.navigation_graf);
if (!isLogin) {
graph.setStartDestination(R.id.action_profile_dest_to_login_dest);
navController.navigate(R.id.menu_profile);
} else {
graph.setStartDestination(R.id.menu_profile);
navController.navigate(R.id.authenticate);
}
}
}
@AndroidEntryPoint 公共类 MainActivity 扩展了 BaseActivity {
private ActivityMainBinding binding;
private boolean isLogin = false;
private String authToken;
@Inject
ProfileFragment profileFragment;
@Inject
UnAuthenticatedFragment authenticatedFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
BottomNavigationView bottomNavigationView = binding.bottomNavigationBar;
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
NavInflater navInflater = navController.getNavInflater();
NavGraph graph = navInflater.inflate(R.navigation.navigation_graf);
if (!isLogin) {
graph.setStartDestination(R.id.action_profile_dest_to_login_dest);
navController.navigate(R.id.menu_profile);
} else {
graph.setStartDestination(R.id.menu_profile);
navController.navigate(R.id.authenticate);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<data>
<variable
name="toolbarViewModel"
type="com.ostovane.android.viewmodel.ToolbarViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:itemIconTint="@drawable/bottom_nav_color"
app:itemTextAppearanceActive="@style/Widget.BottomNavigationView"
app:itemTextAppearanceInactive="@style/Widget.BottomNavigationView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_navigation_menu_item" />
<fragment
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:layout_constraintBottom_toTopOf="@id/bottomNavigationBar"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/navigation_graf" />
</androidx.constraintlayout.widget.ConstraintLayout>