Into my project I am using viewpager with three tabs named History
,Main
,Map
.Main activity contain Timer,stopwatch,etc
if you are working with FragmentPagerAdapter then just use destroyItem method inside FragmentPagerAdapter.
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
FragmentTransaction ft=fm.beginTransaction();
ft.remove((Fragment) object);
ft.commit();
}
setOffscreenPageLimit(int limit) consumes more memory and in some cases its not works perfectly. its retain state in either side see description.setOffscreenPageLimit(int limit) - Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.
I had the same problem when I used
View res = inflater.inflate(R.layout.fragment_guide_search, container);
inside Fragment.onCreateView(...
You must call
View res = inflater.inflate(R.layout.fragment_guide_search, container, false);
or
View res = inflater.inflate(R.layout.fragment_guide_search, null);
First, the answer by RB Patel to insert code (shown below), worked like a charm, but as I read about this feature, I realized it was intended as a potential optimization and isn't actually the solution to the bug.
mViewPager.setOffscreenPageLimit(3);
The Android Developer site says the following about this method:
setOffscreenPageLimit(int limit) - Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.
It seems like this just sets a limit on the number of pages cached to the right or left of the current page. In my case this was just a band aid for the problem, yes the error stopped and things worked smoothly, but I still had bad code lurking in my program which simply not being executed. I believe this method was intended to be used as an optimization and not an actual fix to the bug.
I ran into this problem when trying to overload the instantiateItem() method in a custom class which extends PagerAdapter. My error was thrown after I would navigate back to a tab/page which I had previously visited/instantiated.
I am new to Android but I'll do my best to explain my solution. The line which throw this error was the following:
v.addView(child);
If I understand this correctly, it was choking because the child was already instantiated previously and assigned a parent.
When I would navigate back to a previously visited view/page I was trying to add a view which already had a parent layout view to a new Layout, hence the error.
Solution: I just checked to see if the child view I was trying to display (instantiate) was already instantiated somewhere else by seeing if it had a parent view set. If the child view had already been created, then I just used the parent of the child view instead of creating a new Layout.
Here is my code (I apologize if it offends any experts):
@Override
public Object instantiateItem(View container, int position) {
LinearLayout v = new LinearLayout(mContext);
//the view being loaded (unless it already was then we have to handle it)
View child = mViews.get(position);
//If the child view was loaded already then it will have a parent view it belongs to
//We return the parent view instead of adding the child to the LinearLayout we just created and returning that new layout.
if(child.getParent() != null) {
View parent = (View) child.getParent();
((ViewPager) container).addView(parent);
return parent;
}
//first time instantiating a child view/page
v.addView(child);
// These lines execute the first time a given page is instantiated
((ViewPager) container).addView(v);
return v;
}
For a adapter if you are using with tabLayout or viewPager, instead of getChildFragmentManager()
replace it with getActivity().getSupportFragmentManager()
like below.
TabAdapter adapter = new TabAdapter(getActivity(), getActivity().getSupportFragmentManager());
@Override
public Object instantiateItem(View arg0, int arg1) {
Log.d("instantiateItem", ""+arg0+" "+arg1);
try {
if(mListViews.get(arg1).getParent()==null)
((ViewPager) arg0).addView(mListViews.get(arg1), 0);
else{
// I am new to android, it is strange that the view to be added is already bound to a parent
// Through trials and error I solve this problem with the following codes
// Add that the element of mlistviews is listview in pagerview;
((ViewGroup)mListViews.get(arg1).getParent()).removeView(mListViews.get(arg1));
((ViewPager) arg0).addView(mListViews.get(arg1), 0);
}
} catch (Exception e) {
Log.d("parent=", ""+mListViews.get(arg1).getParent());
e.printStackTrace();
}
return mListViews.get(arg1);
}
// if the child view to be added is already bound to a parent, then remove the child from the parent and then add again to the container.
if (v.getParent() != null) {
((ViewGroup) v.getParent()).removeView(v);
}
container.addView(v);