0

How to remove the fragment from his calling fragment ?

i have search this thing but i didn't get solution according to my requirement. In my application, there is one FragmentActivity which has the viewPager.

This viewPager contains 3 Fragments. For fragment I am using FragmentStatePagerAdapter.

Suppose there is 3 fragment: A, B, C; and D fragment C also contain a child fragment E.

I have call fragment E in the onCreateView() method and in the onDestroy() method of fragment C, I have remove the child fragment E.

So what is happening when I slid viewPager from C to B, and B to A - when I come back from A to B, now current displaying fragment is B. Now if I slid fragment C should be display, but in place of fragment C the child fragment of C, fragment E is displaying after then fragment E then fragment C is displaying now in this condition i have not seen Fragment E over the fragment C, the child fragment E is not interacting with viewpager but why it is added in viewPager,

i have try to destroy the child fragment E in onPause() and in onDestroy() method of C, but nothing is happening. Please any one help me.

@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;
}
rusted brain
  • 1,052
  • 10
  • 23
ramkrishna kushwaha
  • 380
  • 1
  • 6
  • 17
  • 1
    I think you should use custom viewgroups and a normal `ViewPager` instead of `FragmentPagerAdapter` or `FragmentStatePagerAdapter`, that way you will be in direct control of all state. – EpicPandaForce Apr 13 '16 at 11:35
  • @EpicPandaForce the fragment 'MYScannerParentFragment' contain the fragment 'E', fragment 'E' not related to 'viewPager' but after sliding fragment 'E' removed from 'MYScannerParentFragment' and displaying left side the 'MYScannerParentFragment'. – ramkrishna kushwaha Apr 13 '16 at 11:58

2 Answers2

0

You Please try these.

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

                fragmentTransaction.remove(currentFragment);//remove current fragment
Ganesh Gudghe
  • 1,327
  • 1
  • 17
  • 41
0

So what is happening when I slid viewPager from C to B, and B to A - when I come back from A to B, now current displaying fragment is B.

I had faced something before like you have now. I was registering and unregistereing a listener on fragments life cycle events. But in viewpager, even i switched to another fragment, the previously fragment never be destroyed, you can check

Before dive into your case, i wanna mention about 'off page limit'. So you cant set to zero. Check this link. So when you set 0, it defaults to 1

viewpager.setOffscreenPageLimit(0);

If im not missing something, in this case, when you switch from C to B

  • If you had visited D fragment before C, D will be destroyed, B will be created, C still will be living.

Assuming you coming from first case (C to B, and then B to A)

  • C will be destroyed, B lives, A will be created

Bottom line, with ViewPager you have min two fragments. One is you are currently interacting and second (screen off)

Community
  • 1
  • 1
blackkara
  • 4,900
  • 4
  • 28
  • 58
  • @ Blackkara you are not getting my problem let me explain you the Fragment C contain fragment E and E is not related to viewPager . when fragment C is visible is call the Fragment E inside the onCreateView method of C. problem is that when Fragment C has destroyed by viewPager the fragment E which is called by C is not destroyed it is automatically added left side the Fragment C. so requirement is that when C has destroyed E should be destroyed again when C become visible E should also visible over the C. – ramkrishna kushwaha Apr 14 '16 at 04:40
  • So my post refers that FragmentC have to be responsible for creating and removing FragmentE in your case. Add dynamically fragment E when Fragment C appeared, then remove dynamically fragment E when fragment C disappeared. – blackkara Apr 14 '16 at 14:35
  • yes it is my requirements what fragment E automatically removed from C and added left side the C why it is happening? – ramkrishna kushwaha Apr 14 '16 at 18:33