【问题标题】:Android Bottom View Activity multiple instances of fragmentsAndroid底视图Activity片段的多个实例
【发布时间】:2020-04-03 17:57:16
【问题描述】:

我使用底部视图活动创建了一个应用。我的问题是使用片段的多个实例。我已经编写了片段的onPause和onResume来开发活动生命周期,但是在片段之间切换时会创建每个片段的两个实例,因此这些功能不会 正常工作。

我见过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?

【问题讨论】:

    标签: android android-fragments android-activity android-fragmentactivity bottomnavigationview


    【解决方案1】:

    你需要使用:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多