如何從他的調用片段中刪除片段?如何從其調用片段中刪除片段?
我有搜索這個東西,但我沒有得到解決方案根據我的要求。在我的申請中,有一個FragmentActivity
,其中有viewPager
。
這個viewPager
包含3個片段。對於片段,我使用FragmentStatePagerAdapter
。
假設有3個片段:A
,B
,C
;和D
片段C
也包含兒童片段E
。
我在onCreateView()
方法調用片段E
和片段C
的onDestroy()
方法,我已經刪除子片段E
。
所以發生了什麼事,當我滑到viewPager
從C
到B
,並B
到A
- 我回來的時候從A
到B
,現在是當前顯示片段是B
。現在,如果我滑片段C
應該是顯示器,但在地方片段C
的C
孩子片段,片段E
正在顯示後然後分段E
然後片段C在這種情況下我還沒有看到片段E的片段C
現在顯示,孩子片段E
不與viewpager
互動,但爲什麼它在viewPager添加,
我有試圖摧毀孩子片段E
在onPause()
並在C
onDestroy()
方法,但什麼也沒有發生。請任何人幫助我。
@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;
}
我認爲你應該使用自定義viewgroups和一個普通的'ViewPager'而不是'FragmentPagerAdapter'或'FragmentStatePagerAdapter',這樣你將直接控制所有的狀態。 – EpicPandaForce
@EpicPandaForce片段'MYScannerParentFragment'包含片段'E',片段'E'與'viewPager'無關,但在從'MYScannerParentFragment'中刪除滑動片段'E'並在左側顯示'MYScannerParentFragment'。 –