Android 底部视图 Activity 片段的多个实例

Android Bottom View Activity multiple instances of fragments

我使用底部视图创建了一个应用 Activity。我的问题是使用片段的多个实例。我已经编写了片段的 onPause 和 onResume 来开发 activity 生命周期,但是当在片段之间切换时,每个片段都会创建两个实例,所以这些函数不会 正常工作。

我看过this,但我的主要是这样的:

public class Main extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    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_home, R.id.navigation_dashboard, R.id.navigation_notifications)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
    NavigationUI.setupWithNavController(navView, navController);
}
}

这里如何实现FragmentManager?

您需要使用:

getSupportFragmentManager().beginTransaction().replace(
        R.id.your_fragment_container,
        YourFragment(),
        YourFragmentsTag
)