ShniobiChart in FragmentStatePagerAdapter


I am trying to make swappable charts (swapping by finger to move to different charts). I am trying to do this using FragmentStatePager, which provides swapping to switch to different Fragment.  Here, we use one layout xml because all pages have the same format. Now, I put Fragment seciton there. However, because it has one ID for the fragment, it causes error when the FragementStatePagerAdapter is trying multiple pages. 

Is there any way or example how this can be achieved? The number of pages is not fixed. User will select data sets. I then need to create a chart for each set.


Hi myungchoi,

If I’ve understood correctly the issue here isn’t related to specifically to our ChartFragment but actually to the way you are trying to use Fragments.

It sounds like you’re trying to nest a Fragment within a Fragment by specifying it in the XML. As the Android developer docs state “you cannot inflate a layout into a fragment when that layout includes a <fragment>. Nested fragments are only supported when added to a fragment dynamically”. See

Essenatially, the issue is that when the first page-level Fragement is created, the Fragment inside is also created with the id specified in XML. However, when the second page-level Fragment is created, as it’s using the same XML layout file, it attempts to create another (inner) Fragment with the same id as the first inner Fragment. And that first Fragment is still around as the Activity has not been destroyed.

Please note that nested fragments were only introduced in Android 4.2 (though there is provision in the support library to provide backwards compatibility) and that currently our ChartFragment does not support being nested inside another Fragment.

So, back to your question: how do you have swappable charts? There are a couple of options open to you:

  1. Use a ChartView instead of a ChartFragment in your XML layout.

You’d have to ensure you forward on the lifecycle calls from the Fragment to the ChartView (e.g. override onPause() in you fragment to call super.onPause() and then chartView.onPause()) - see

The Fragment, and thus the ChartView, won’t be retained over configuration changes so you’ll need to handle this - essentially you’re creating a new ChartView every config change so it just really depends on how you’re setting up your chart and pushing data into it.

  1. Subclass ChartFragment and override the onCreate() method to setup your chart. Then just instantiate this class rather than the page-level Fragments.

If your page-level Fragments contained additional views around the ChartFragment then this approach probably won’t be as suitable but if not then it could be quite neat.

I hope that helps,



Patrick,  do you have any example on subclassing ChartFragment? I am extending ChartFragment. But, I am getting null for getting shinobi object. I thought calling super class in override onCreate should set up the shinobi object. It does not seem to do it. If you have any example, it would help me.


Hi myungchoi,

There is an example of subclassing ChartFragment in the CustomDataAdapter sample app included in the download bundle (see

It’s not just a case of overriding onCreate and calling super - even with a subclassed ChartFragment if you call getShinobiChart() before the onCreate() method of the ChartFragment has run you will get back null. The key is to set up your ShinobiChart inside the onCreate() method of your ChartFragment subclass.

Hope that helps,