如何正确处理底部导航视图中片段的切换

huangapple 未分类评论50阅读模式
英文:

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

  1. The fragment recreates each time you switch between fragments

  2. 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;
    }

huangapple
  • 本文由 发表于 2020年6月5日 22:04:14
  • 转载请务必保留本文链接:https://java.coder-hub.com/62217174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定