英文:
How to properly handle switching of fragments in bottom navigation view
问题
我使用了 Android 示例活动创建了一个底部导航视图,但从外观上看,我认为它没有正确实现,因为我注意到了两件事情:
1. 在切换片段时,片段每次都会重新创建。
2. 有时在快速切换片段时,应用程序会崩溃,并显示以下错误:
`java.lang.IllegalStateException: Fragment TransactionsFragment{6b18628} (462273e7-ed16-4e50-93f2-935432434a9b)} 尚未被附加。`
我的问题是,有没有办法防止每次都重新创建片段?
还有,在第2个问题中如何处理这个错误?
**MainActivity.java**
```java
public class MainActivity extends AppCompatActivity {
private static BottomNavigationView navView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navView = findViewById(R.id.nav_view);
addBadgeView();
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_transactions, R.id.navigation_wallets, R.id.navigation_account)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
// NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
private void addBadgeView() {
navView = findViewById(R.id.nav_view);
navView.getOrCreateBadge(R.id.navigation_transactions); // Show badge
}
}
Fragment.java(这是我在onCreate中实例化片段的方式)
public class HomeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
private static String TAG = HomeFragment.class.getSimpleName();
private HomeViewModel homeViewModel;
public HomeFragment() {
}
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
final View root = inflater.inflate(R.layout.fragment_home, container, false);
return root;
}
}
英文:
I created a bottom navigation view using the Android sample activity, but from the looks of things I don't think it's properly implemented as i've noticed 2 things
-
The fragment recreates each time you switch between fragments
-
Sometimes when you switch fragments too fast thee app crashes with this error;
java.lang.IllegalStateException: Fragment TransactionsFragment{6b18628} (462273e7-ed16-4e50-93f2-935432434a9b)} has not been attached yet.
My question here is there a way to prevent the fragment recreation each time?
And how can I handle that error in 2?
MainACtivity.Java
public class MainActivity extends AppCompatActivity {
private static BottomNavigationView navView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navView = findViewById(R.id.nav_view);
addBadgeView();
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_transactions, R.id.navigation_wallets, R.id.navigation_account)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
// NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
private void addBadgeView() {
navView = findViewById(R.id.nav_view);
navView.getOrCreateBadge(R.id.navigation_transactions); // Show badge
}
}
Fragment.java (this is how I instantiate a fragment in oncreate)
public class HomeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
private static String TAG = HomeFragment.class.getSimpleName();
private HomeViewModel homeViewModel;
public HomeFragment() {
}
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
final View root = inflater.inflate(R.layout.fragment_home, container, false);
return root;
}
专注分享java语言的经验与见解,让所有开发者获益!
评论