I have an activity that has a ViewPager and a FragmentStatePagerAdapter. Each page displayed by the activity is a single fragment. Each fragment has different content.
I'm adding App Indexing to this activity. I've read lots of stuff including https://developers.google.com/app-indexing/android/publish#add-app-indexing-api-calls. This link says:
Regarding fragments: You structure fragments in the same way as the activity in the example above. But because fragments may execute many times within an activity, or there may be multiple fragments, you should take care to make the API call only once. Here are some guidelines:
- If the activity calls the API, then don't call the API again from any fragment within the activity.
- If the activity doesn't call the API, and you want a fragment to call it instead, then make sure only one fragment calls the API and only one time.
This google developer page says the activity should call the API only once. If only called once however I can only index one page of content. I'd like to be able to call the API every time the ViewPager shows a new fragment.
Is there a way to do this? I can find no documentation describing how to index content displayed by paging a ViewPager within an activity.
I'd like to be able to do something like this.
1.In the activity onCreate call mClient = new
GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
2. In activity onCreate call mClient.connect().
3. In the activity when a fragment becomes visible call AppIndex.AppIndexApi.start(mClient, myAction)
4. In the activity when a fragment becomes invisible call AppIndex.AppIndexApi.end(mClient, myAction)
5. In activity onDestroy call mClient.disconnect()
4. Repeat steps 3 and 4 as new views/fragments are paged in by the ViewPager
This design would result in multiple AppIndexApi calls in a single activity, violating (as I interpret) the google documentation. Even if I do implement this I'll have no confidence it actually properly indexes my content. Would code like I've described above work? Can you suggest a better way of indexing in this scenario. Or am I just horribly confused by what App Indexing is doing?