Resolved: TabRow/Tab Recomposition Issue in Compose Accompanist Pager

In this post, we will see how to resolve TabRow/Tab Recomposition Issue in Compose Accompanist Pager


I was trying to create a sample Tab View in Jetpack compose, so the structure will be like Inside a Parent TabRow we are iterating the tab title and create Tab composable.
More precise code will be like this.
And coming to my question is whenever we click on the tab item, the inner content get recompose so weirdly. Im not able to understand why it is happens.
Am attaching an image of the recomposition counts below, please take a look that too, it would be good if you guys can help me more for understand this, also for future developers.
There are two question we have to resolve in this stage
  1. Whether it will create any performance issue, when the view getting more complex
  2. How to resolve this recompostion issue

Thanks alot.
enter image description here

Best Answer:

… whenever we click on the tab item, the inner content get recompose so weirdly. Im not able to understand why it is happens…

It’s hard to determine what this “weirdness” is, there could be something inside the composable your’e mentioning here.
You also didn’t specify what the API is, so I copied and pasted your code and integrated accompanist view pager, then I was able to run it though not on an Android Studio with a re-composition count feature.
And since your’e only concerned about the Text and the Icon parameter of the API, I think that’s something out of your control. I suspect the reason why your’e getting those number of re-composition count is because your’e animating the page switching.
Though ‘m not able to try this on another Android Studio version with the re-composition feature, I think (though I’m not sure) scrolling to another page without animation will yield less re-composition count.
If it still bothers you, the best course of action is to ask them directly, though personally I wouldn’t concerned much about this as they are part of an accepted API and its just Text and Icon being re-composed many times by an animation which is also fine IMO.
Now if you have some concerns about your PagerSampleItem stability(which you have a full control), based on the provided code and screenshot, I think your’e fine.
There’s actually a feature suggested from this article to check the stability of a composable, I run it and I got this report.
Everything about this report is within the article I linked.
Also, your Text and Icon are using String and ImageVector which is stable and immutable (marked by @Immutable) respectively.
So TLDR, IMO your code is fine, your PagerSampleItem is not re-composing in the screenshot.

If you have better answer, please add a comment about this, thank you!