2017-09-01 40 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