【发布时间】:2021-09-07 18:10:04
【问题描述】:
我为一个学校项目制作了一个android应用程序,实际上我遇到了一个问题。
我创建了 2 个活动(MainActivity 和 BottomNavActivity),我尝试在它们之间导航,但导航中的“片段”主页仍未定义。 我发现本教程是为了在片段之间导航 我添加了事件):
https://medium.com/@oluwabukunmi.aluko/bottom-navigation-view-with-fragments-a074bfd08711
但是,现在,我可以在我的“片段”之间导航,但我最初的“片段”(启动活动时首先出现的片段)仍然存在,因此在更改片段时,它会被复制。
我的 Infos.class
public class Infos extends AppCompatActivity {
private ActivityInfosBinding binding;
//I create my variable
final Fragment frag2 = new ProduitsFragment();
final Fragment frag1 = new EntrepriseFragment();
final Fragment frag3 = new CGUFragment();
final FragmentManager fm = getSupportFragmentManager();
Fragment active = frag1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityInfosBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_entreprise, R.id.navigation_produits, R.id.navigation_CGU)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_infos);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(binding.navView, navController);
//I add my differents fragments
fm.beginTransaction().add(R.id.nav_host_fragment_activity_infos, frag3).hide(frag3).commit();
fm.beginTransaction().add(R.id.nav_host_fragment_activity_infos,frag1).hide(frag1).commit();
navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem item) {
switch (item.getItemId()) {
//My first fragment
case R.id.navigation_entreprise:
fm.beginTransaction().hide(active).show(frag1).commit();
active = frag1;
return true;
//My second fragment (default in video)
case R.id.navigation_CGU:
fm.beginTransaction().hide(active).show(frag3).commit();
active = frag3;
return true;
}
return false;
}
});
}
}
还有我的 mobile_navigation.xml
<?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/mobile_navigation"
app:startDestination="@id/navigation_CGU"> //I try to change this, but nothing. This fragment is duplicated.
<fragment
android:id="@+id/navigation_entreprise"
android:name="fr.romaindrouhot.aurelinfo.ui.home.EntrepriseFragment"
android:label="@string/nav_entreprise"
tools:layout="@layout/fragment_entreprise" />
<fragment
android:id="@+id/navigation_produits"
android:name="fr.romaindrouhot.aurelinfo.ui.home.ProduitsFragment"
android:label="@string/nav_produits"
tools:layout="@layout/fragment_produits" />
<fragment
android:id="@+id/navigation_CGU"
android:name="fr.romaindrouhot.aurelinfo.ui.home.CGUFragment"
android:label="@string/nav_CGU"
tools:layout="@layout/fragment_cgu" />
</navigation>
你能帮帮我吗?
【问题讨论】:
-
好的,在那个帖子下看到
-
您好,您是否尝试删除评论下方的所有代码
//I add my differents fragments?通常,导航组件处理片段事务
标签: android android-fragments duplicates