YouTube 如何处理其标签片段的生命周期?当我点击选项卡时,即使我切换了该选项卡,它也会开始并继续加载数据。当我再次重新选择该选项卡时,它不会重新加载它的视图和数据。我认为有一些布尔变量,如 didFeedLoad,然后在 onCreate 中设置为 true?但在我看来,这不是优雅和好的解决方案。你怎么看?
在我的应用程序而不是 ViewPager 和 TabLayout 中,我有 BottomNavigationView,但我想像在 YouTube Android 应用程序中一样处理生命周期
下面是导航代码:
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, new OneFragment());
fragmentTransaction.commit();
BottomNavigationView bottomNavigationView =
(BottomNavigationView) findViewById(R.id.bottom_navigation_view);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.action_one:
fragment = new OneFragment();
break;
case R.id.action_two:
fragment = new TwoFragment();
break;
case R.id.action_three:
fragment = new ThreeFragment();
break;
case R.id.action_four:
fragment = new FourFragment();
break;
case R.id.action_five:
fragment = new FiveFragment();
break;
}
if (fragment != null) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, fragment);
fragmentTransaction.commit();
}
return true;
}
});