2017-09-01 38 views
1

我已经实现了一个有一些步骤的公式。每一步都在新的Fragment中使用FrameLayout来自相机的Android丢失片段视图

在最后一步(FRAGMENT A)有一个Button。点击此Button时,会启动新的FragmentFRAGMENT B)和相机Intent

有时照片拍摄后,相机被驳回后,该FRAGMENT A显示,而不是FRAGMENT B。发生这种情况时,UI元素将被冻结,任何字段都可以固定,并且继续使用该应用程序的唯一方法是关闭Form并再次启动该过程。

我以为这是一个OOM错误,所以Activity被杀死/恢复,最后一个状态没有正确存储。

我试着检查,但方法onRestoreInstanceState()未被调用。无论是否显示,相机关闭后,也会调用FRAGMENT B中的方法。这就是我所谓的打开FRAGMENT B代码和相机:

碱基的片段

private void setCurrentFragment(int pos, boolean animate) { 
    showFragment(buildFragment(mCurrentPage), animate, animationFromRight); 
    .... 
} 

private Fragment buildFragment(int pos) { 
    switch (mHasFamilyManager ? pos : pos + 1) { 
     ............ 
     case 3: 
      mCurrentFragment = PhotoVerificationFragment.newInstance(); 
      if (mState.getAttachments().isEmpty()) { 
       switch (mState.getPictureSelector()) { 
        ..... 
        case CAMERA: 
         onAddCameraPictureClicked(); 
         break; 
       } 
      ..... 

    return mCurrentFragment; 
} 

private void showFragment(final Fragment fragment, boolean animate, 
     final boolean animationFromRight) { 
    if (fragment != null) { 
     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
     if (animate) { 
      if (animationFromRight) { 
       transaction.setCustomAnimations(R.anim.activity_slide_in_right, 
         R.anim.activity_slide_out_left); 
      } else { 
       transaction.setCustomAnimations(R.anim.activity_slide_in_left, 
         R.anim.activity_slide_out_right); 
      } 
     } 
     transaction.addToBackStack(null); 
     transaction.replace(R.id.container, fragment); 
     transaction.commit(); 
    } 
} 

MainActivity

public void onAddCameraPictureClicked() { 
    startActivityForResult(takePictureIntent, requestCode); 
    ..... 
} 

是否有人有一个想法?提前致谢!

回答

1

请试试这个:

transaction.add(R.id.container, fragment); 
transaction.disallowAddToBackStack(); 
transaction.commit(); 

使用replace你是在彼此的顶部更换片段,所以它不会删除以前的片段,或者是将低于目前的一个新片段。

使用add确保它始终位于顶部。

disallowAddToBackStack将确保您没有任何东西在堆栈中。

+1

太棒了!它的作品像一个魅力,非常感谢:) – IrApp

+1

很高兴它帮助你;) – Ale