Search code examples
androidgraphonresume

Pie Graph is drawing slices with the slices that have been already created when the activity is resumed. How do I prevent or fix this issue?


I am using HoloGraphLibrary and using data from an adapter to generate slices. The graph works perfectly when the activity is first created. The issue arises when the activity is resumed. I posted two pictures, I believe they show the issue better than I can explain it. First Image is of the graph when it is first created and the second is when the activity is resumed. I have tried to invalidate the graph with no success. How would I recreate the graph when the activity is resumed so it does not draw the graphs with the graphs that already exist? If you need more information, I can post more code.

Implementation of Graph

@Override
protected void onResume() {
    super.onResume();
    adapters();
    pieGraph();
}

PieGraph pg;
public void pieGraph() {
    pg = (PieGraph) findViewById(R.id.graph);
    for (int x = 0; x < mSecondAdapter.getCount(); x++) {
        LogSecond lti = mSecondAdapter.getItem(x);
        String color = lti.getColorCode();
        String sdf = dff.format(lti.getTotal());
        PieSlice slice = new PieSlice();
        slice.setColor(Color.parseColor(color));
        slice.setValue(Integer.valueOf(sdf));
        pg.addSlice(slice);
        pg.forceLayout();
        pg.invalidate();
    }
}

OnCreare enter image description here OnResume enter image description here


Solution

  • You could call removeslices.

    pg = (PieGraph) findViewById(R.id.graph);
    pg.removeslices();
    for (int x = 0; x < mSecondAdapter.getCount(); x++) {
        LogSecond lti = mSecondAdapter.getItem(x);
        String color = lti.getColorCode();
        String sdf = dff.format(lti.getTotal());
        PieSlice slice = new PieSlice();
        slice.setColor(Color.parseColor(color));
        slice.setValue(Integer.valueOf(sdf));
        pg.addSlice(slice);
        pg.forceLayout();
        pg.invalidate();
    }