0

I am currently working on an app that displays multiple charts on multiple fragments, which are viewed via a view pager. When the charts are first drawn the background image is calculated and drawn to a bitmap image which is then drawn to the chart background. At the moment this all works fine but after sliding through all of the fragments the background starts to disappear.

I would like to know if this a view pager issue using fragments and images or an android issue using bitmap, I know that is a difficult general question that is hard to answer but has anyone come across this before. I am also looking at the invalidate method and the fact that are the initial draw maybe that is not being called on the re-swipe. Any help, tips or pointers welcome.

CDVN
  • 171
  • 1
  • 1
  • 11

1 Answers1

1

i always add a boolean to the onCreateView method, like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  if (!mInitialized) {
    mView = inflater.inflate(...);
    ...    
    mInitialized = true;
  }    
  return mView;
}

so you avoid re-initializing the fragments on each swipe. maybe this helps.

EDIT:

As DVN mentioned in the comments, if the following exception occurs

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first

you can find some hints about it here: java.lang.IllegalStateException: The specified child already has a parent

Community
  • 1
  • 1
Rich
  • 1,015
  • 1
  • 10
  • 22
  • hey thanks for the tip, however I ran into another issue after implementing your code: _java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first_ – CDVN Jun 24 '15 at 11:53
  • 1
    I added the following code to my fragment to fix that issue '@Override public void onDestroyView() { super.onDestroyView(); if (mView != null) { ViewGroup parentViewGroup = (ViewGroup) mView.getParent(); if (parentViewGroup != null) { parentViewGroup.removeAllViews(); } } }' [link](http://stackoverflow.com/questions/10007094/java-lang-illegalstateexception-the-specified-child-already-has-a-parent). For anyone who might need it. – CDVN Jun 24 '15 at 11:58