0

Im using my Custom FragmentAdapter to hold the fragment into a List

The Process i want:

1) User click in a list item and it create a new Activity within arguments and than create the fragment using the TagsPageAdapter.addPage(fragment);

2) This created fragment has a "id" int inside his arguments

3) When i hit the addPage, it verify all the fragments inside the List to check if any fragments has the id, if yes, restore it, if not, than create new and add to the list

this 3 steps are working perfeclty, the problem is when i try to remove one (error after the adapter code below)

adapter:

private class TabsPagerAdapter extends FragmentPagerAdapter {

        private Bundle bundle;
        private String titulo;


        private TabsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public LeiAberta getItem(int position) {
            current = fragments.get(position);
            return current;
        }

        private void addPage(LeiAberta page) {
            boolean found = false;
            for (int i = 0; i < fragments.size(); i++) {
                leiAberta = fragments.get(i);
                if (leiAberta != null) {
                    if (leiAberta.getArguments() != null) {
                        if (leiAberta.getArguments().getInt("id") == page.getArguments().getInt("id")) {
                            viewPager.setCurrentItem(i);
                            found = true;
                        }
                    }
                }
            }
            if (!found) {
                fragments.add(page);
                notifyDataSetChanged();
                viewPager.setCurrentItem(fragments.size() - 1);
            }
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            this.bundle = fragments.get(position).getArguments();
            if (this.bundle != null) {
                this.titulo = bundle.getString("titulo");
            }
            return titulo;
        }
    }

what im using to remove the fragment from the list:

     int current_position = viewPager.getCurrentItem();
        if(current_position == 0) {
     viewPager.setCurrentItem(current_position +1);
        } else {
    viewPager.setCurrentItem(current_position -1);
        }
fragments.remove(current_position);
        adapter.notifyDataSetChanged();

if i remove the current tab/fragment, and then open another item in the list that will add another tab, no matter what item, it just restores the old fragment in the same position, for some reason the position of the removed fragment in the List keep filled in the shadows, and the strange thing is: the list.size(); decrease when i remove, so WHY? From where is this old fragment restored? how can i really destroy it, so the addPage doesnt find it in the "shadows"

user2582318
  • 1,607
  • 5
  • 29
  • 47

1 Answers1

0

i had tried:

  1. Update ViewPager dynamically?
  2. Removing fragments from FragmentStatePagerAdapter
  3. Remove Fragment Page from ViewPager in Android
  4. How to remove pages in Android ViewPager?

and many others

apparently the only solution was this framework:

https://github.com/inloop/UpdatableFragmentStatePagerAdapter?utm_source=android-arsenal.com&utm_medium=referral&utm_campaign=4851

using this override

@Override
public int getItemPosition(Object object) {
    if (fragments.contains(object)) return fragments.indexOf(object);
    else return POSITION_NONE;
}
Community
  • 1
  • 1
user2582318
  • 1,607
  • 5
  • 29
  • 47