[Android 开发]Fragment 崩溃后不能切换

在Android开发中用Fragment 作为主界面,在二级界面崩溃后自动回到主界面

然后不能fragment 不能点击切换,解决方法如下:

在切换时间中套一个Handler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Handler clickHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            replaceFragment();
        }
    };
private void replaceFragment(Fragment toFragment) {
    this.toFragment = toFragment;
    clickHandler.sendEmptyMessage(0);
}
 private void replaceFragment() {
     FragmentTransaction trasection = getSupportFragmentManager()
            .beginTransaction();
    if (!toFragment.isAdded()) {
      try {
              trasection.add(R.id.fragment_container, toFragment);
              trasection.hide(currentFragment);
              trasection.show(toFragment);
              trasection.commit();
          } catch (Exception e) {
            }
    } else {
           trasection.hide(currentFragment);
           trasection.show(toFragment);
           trasection.commit();
       }
   currentFragment = toFragment;
}