- // Get the new tab position.
- val newTabPosition = if (adjacent) // The new tab position is immediately to the right of the current tab position.
- tabLayout.selectedTabPosition + 1
- else // The new tab position is at the end. The tab positions are 0 indexed, so the new page number will match the current count.
- tabLayout.tabCount
+ // Add the new tab after the tab layout has quiesced.
+ // Otherwise, there can be problems when restoring a large number of tabs and processing a new intent at the same time. <https://redmine.stoutner.com/issues/1136>
+ tabLayout.post {
+ // Get the new tab position.
+ val newTabPosition = if (adjacent) // The new tab position is immediately to the right of the current tab position.
+ tabLayout.selectedTabPosition + 1
+ else // The new tab position is at the end. The tab positions are 0 indexed, so the new page number will match the current count.
+ tabLayout.tabCount