2016-04-13 152 views
0

如何從他的調用片段中刪除片段?如何從其調用片段中刪除片段?

我有搜索這個東西,但我沒有得到解決方案根據我的要求。在我的申請中,有一個FragmentActivity,其中有viewPager

這個viewPager包含3個片段。對於片段,我使用FragmentStatePagerAdapter

假設有3個片段:AB,C;和D片段C也包含兒童片段E

我在onCreateView()方法調用片段E和片段ConDestroy()方法,我已經刪除子片段E

所以發生了什麼事,當我滑到viewPagerCB,並BA - 我回來的時候從AB,現在是當前顯示片段是B。現在,如果我滑片段C應該是顯示器,但在地方片段CC孩子片段,片段E正在顯示後然後分段E然後片段C在這種情況下我還沒有看到片段E的片段C現在顯示,孩子片段E不與viewpager互動,但爲什麼它在viewPager添加,

我有試圖摧毀孩子片段EonPause()並在ConDestroy()方法,但什麼也沒有發生。請任何人幫助我。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    MYDataManager.sharedHandler().changeLanguageConfiguration(); 
    overridePendingTransition(R.anim.activity_open_translate, R.anim.activity_close_scale); 
    setContentView(R.layout.activity_myscanner); 

    appFlow = new ArrayList(Arrays.asList(MYConstant.kProfileMenuSettings, MYConstant.kScanner, MYConstant.kRestaurantListing)); 

    viewpager = (ViewPager) findViewById(R.id.view_pager); 
    viewpager.addOnPageChangeListener(this); 
    viewpager.setOffscreenPageLimit(0); 

    reloadViewPager(1); 
} 

public void reloadViewPager(int currentItem) { 
    adapter = new FragmentStatePagerAdapter(getSupportFragmentManager()) { 
     @Override 
     public Fragment getItem(int position) { 
      return getFragmentWithPosition(position); 
     } 

     @Override 
     public int getCount() { 
      return appFlow.size(); 
     } 

     @Override 
     public int getItemPosition(Object object) { 
      return POSITION_NONE; 
     } 
    }; 
    viewpager.setAdapter(adapter); 
    viewpager.setCurrentItem(currentItem); 
} 

private MYBaseFragment getFragmentWithPosition(int position) { 
    String screen = appFlow.get(position); 
    MYBaseFragment fragment = null; 
    if(screen.equals(MYConstant.kProfileMenuSettings)) { 
     myProfileSettingFragment = new MYProfileSettingFragment(); 
     fragment = myProfileSettingFragment; 
    } else if(screen.equals(MYConstant.kScanner)) { 
     fragment = new MYScannerParentFragment(); 
    } else if(screen.equals(MYConstant.kRestaurantListing)) { 
     myRestaurantListFragment = new MYRestaurantListFragment(); 
     fragment = myRestaurantListFragment; 
    } else if(screen.equals(MYConstant.kRestaurantDetails)) { 
     myResraurantDetailsFragment = new MYResraurantDetailsFragment(); 
     fragment = myResraurantDetailsFragment; 
    } else if(screen.equals(MYConstant.kCurrentRestaurantDetails)) { 
     MYQRCode qrData = MYDataManager.sharedHandler().getQRData(); 
     if(MYUitilities.checkQRValidation(qrData)) { 
      fragment = new MYCurResDetails(); 
     } 
    } else if(screen.equals(MYConstant.kSettings)) { 
     fragment = new MYSettingFragment(); 
    } else if(screen.equals(MYConstant.kViewMenu)) { 
     fragment = new MYResCatListFragment(); 
    } else if(screen.equals(MYConstant.kMenuCategoryListing)) { 
     fragment = new MYResCatListFragment(); 
    } else if(screen.equals(MYConstant.kViewMenuViewPage)) { 
     // myResMenuViewPagerFragment = new MYResMenuViewPagerFragment(); 
     // fragment = myResMenuViewPagerFragment; 
     fragment = new MYResMenuViewPagerFragment(); 
    } else if(screen.equals(MYConstant.kReviewOrder)) { 
     fragment = new MYReviewOrderFragment(); 
    } else if(screen.equals(MYConstant.kYourOrder)) { 
     fragment = new MYYourOrderFragment(); 
    } else if(screen.equals(MYConstant.kSettings)) { 
     fragment = new MYSettingFragment(); 
    } else if(screen.equals(MYConstant.kOrderHistory)) { 
     fragment = new MYOrderHistoryFragment(); 
    } else if(screen.equals(MYConstant.kCheckout)) { 
     fragment = new MYCheckoutFragment(); 
    } else if(screen.equals(MYConstant.kCallWaiter)) { 
     fragment = new MYCallWaiterFragment(); 
    } else if(screen.equals(MYConstant.kYourProfile)) { 
     myYourProfileFragment = new MYYourProfileFragment(); 
     fragment = myYourProfileFragment; 
    } 

    fragment.setFragmentInteractionListener(MYScannerActivity.this); 
    return fragment; 
} 

@Override 
public void onPageSelected(final int position) { 
    if(currentPosition >= 0) { 
     final String previousScreen = appFlow.get(currentPosition); 
     final String currentScreen = appFlow.get(position); 

     System.out.println("previousScreen-" + previousScreen); 
     System.out.println("currentScreen-" + currentScreen); 

     if(previousScreen.equals(MYConstant.kYourProfile) && currentScreen.equals(MYConstant.kProfileMenuSettings)) { 
      appFlow.remove(MYConstant.kYourProfile); 
      adapter.notifyDataSetChanged(); 
      viewpager.setCurrentItem(0); 
     } 
     if(previousScreen.equals(MYConstant.kRestaurantDetails) && currentScreen.equals(MYConstant.kRestaurantListing)) { 
      appFlow.remove(MYConstant.kRestaurantDetails); 
      adapter.notifyDataSetChanged(); 
     } else if(previousScreen.equals(MYConstant.kViewMenu) && currentScreen.equals(MYConstant.kRestaurantDetails)) { 
      appFlow.remove(MYConstant.kViewMenu); 
      adapter.notifyDataSetChanged(); 
     } else if(previousScreen.equals(MYConstant.kViewMenuViewPage) && currentScreen.equals(MYConstant.kViewMenu)) { 
      appFlow.remove(MYConstant.kViewMenuViewPage); 
      adapter.notifyDataSetChanged(); 
     } else if(previousScreen.equals(MYConstant.kViewMenuViewPage) && currentScreen.equals(MYConstant.kMenuCategoryListing)) { 
      appFlow.remove(MYConstant.kViewMenuViewPage); 
      adapter.notifyDataSetChanged(); 
     } else if(previousScreen.equals(MYConstant.kYourOrder) && currentScreen.equals(MYConstant.kViewMenuViewPage)) { 
      appFlow.remove(MYConstant.kYourOrder); 
      adapter.notifyDataSetChanged(); 
     } else if(previousScreen.equals(MYConstant.kReviewOrder) && currentScreen.equals(MYConstant.kViewMenuViewPage)) { 
      appFlow.remove(MYConstant.kReviewOrder); 
      adapter.notifyDataSetChanged(); 
     } else if(previousScreen.equals(MYConstant.kYourOrder) && currentScreen.equals(MYConstant.kReviewOrder)) { 
      appFlow.remove(MYConstant.kYourOrder); 
      adapter.notifyDataSetChanged(); 
     } else if(previousScreen.equals(MYConstant.kYourOrder) && currentScreen.equals(MYConstant.kViewMenuViewPage)) { 
      appFlow.remove(MYConstant.kReviewOrder); 
      adapter.notifyDataSetChanged(); 
     } else if(previousScreen.equals(MYConstant.kSettings) && currentScreen.equals(MYConstant.kProfileMenuSettings)) { 
      appFlow.remove(MYConstant.kSettings); 
      adapter.notifyDataSetChanged(); 
      viewpager.setCurrentItem(0); 
     } else if(previousScreen.equals(MYConstant.kOrderHistory) && currentScreen.equals(MYConstant.kProfileMenuSettings)) { 
      appFlow.remove(MYConstant.kOrderHistory); 
      adapter.notifyDataSetChanged(); 
      viewpager.setCurrentItem(0); 
     } else if(previousScreen.equals(MYConstant.kCheckout) && currentScreen.equals(MYConstant.kViewMenuViewPage)) { 
      appFlow.remove(MYConstant.kCheckout); 
      adapter.notifyDataSetChanged(); 
     } else if(previousScreen.equals(MYConstant.kCheckout) && currentScreen.equals(MYConstant.kReviewOrder)) { 
      appFlow.remove(MYConstant.kCheckout); 
      adapter.notifyDataSetChanged(); 
     } else if(previousScreen.equals(MYConstant.kCheckout) && currentScreen.equals(MYConstant.kYourOrder)) { 
      appFlow.remove(MYConstant.kCheckout); 
      adapter.notifyDataSetChanged(); 
     } else if(previousScreen.equals(MYConstant.kCallWaiter) && currentScreen.equals(MYConstant.kCheckout)) { 
      appFlow.remove(MYConstant.kCallWaiter); 
      adapter.notifyDataSetChanged(); 
     } else if(previousScreen.equals(MYConstant.kRestaurantListing) && currentScreen.equals(MYConstant.kScanner)) { 
      if(MYDataManager.sharedHandler().isQRCodeScanned()) { 
       appFlow.remove(MYConstant.kRestaurantListing); 
       if(!appFlow.contains(MYConstant.kCurrentRestaurantDetails)) { 
        appFlow.add(MYConstant.kCurrentRestaurantDetails); 
       } 
       adapter.notifyDataSetChanged(); 
      } 
     } else if(previousScreen.equals(MYConstant.kCurrentRestaurantDetails) && currentScreen.equals(MYConstant.kScanner)) { 

     } else if(previousScreen.equals(MYConstant.kMenuCategoryListing) && currentScreen.equals(MYConstant.kCurrentRestaurantDetails)) { 
      appFlow.remove(MYConstant.kMenuCategoryListing); 
      adapter.notifyDataSetChanged(); 
     } 
    } 

    Fragment fragment = ((FragmentStatePagerAdapter) viewpager.getAdapter()).getItem(position); 
    if(fragment instanceof MYProfileSettingFragment) { 

    } 
    currentPosition = position; 
} 
+1

我認爲你應該使用自定義viewgroups和一個普通的'ViewPager'而不是'FragmentPagerAdapter'或'FragmentStatePagerAdapter',這樣你將直接控制所有的狀態。 – EpicPandaForce

+0

@EpicPandaForce片段'MYScannerParentFragment'包含片段'E',片段'E'與'viewPager'無關,但在從'MYScannerParentFragment'中刪除滑動片段'E'並在左側顯示'MYScannerParentFragment'。 –

回答

0

您請試試這些。

FragmentManager fragmentManager = getActivity().getFragmentManager(); 
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
       Fragment currentFragment = fragmentManager.findFragmentById(R.id.frame_container); 

       fragmentTransaction.remove(currentFragment);//remove current fragment 
+0

這個東西不適合我,在這裏播種currentFragment = null。 –

+0

你必須添加名稱到你的分數 –

+0

,我必須添加片段名稱?我已經嘗試過使用標籤,但並沒有刪除片段。 –

0

所以,當我滑到viewPager從C到B B發生了什麼,以及A - 我回來的時候,從A到B,現在是當前顯示片段B.

我像現在這樣面對過一些事情。我正在註冊並註銷關於碎片生命週期事件的偵聽器。但在視圖上,即使我切換到另一個片段,以前的片段永遠不會被銷燬,您可以check

在深入您的案例之前,我想提及一下「關閉頁面限制」。所以你不能設置爲零。檢查link。所以,當你設置爲0,則默認爲1

viewpager.setOffscreenPageLimit(0); 

如果我不是失去了一些東西,在這種情況下,當你從C轉向到B

  • 如果你有前C走訪d片段,D將被銷燬,B將被創造,C仍然會生活。

假設你從第一殼體來(C到B,然後B到A)

  • Ç將被破壞,B住,A將被創建

底線,與ViewPager你有兩分鐘的片段。一個是你正在互動和第二個(屏幕關閉)

+0

@ Blackkara你沒有得到我的問題讓我解釋你的片段C包含片段E和E與viewPager無關。當片段C可見時,在C的onCreateView方法內調用片段E.問題是當片段C被viewPager銷燬時,被C調用的片段E不被銷燬,它會自動添加到片段C的左側。是當C被破壞時E應該在C變得可見時被再次銷燬E應該也可以通過C. –

+0

所以我的帖子提到FragmentC必須負責在你的案例中創建和刪除FragmentE。當片段C出現時動態添加片段E,然後在片段C消失時動態刪除片段E. – Blackkara

+0

是的,這是我的要求什麼片段E自動從C中刪除,並添加左側的C爲什麼它發生? –