super.onDestroy()
}
+ override fun onConfigurationChanged(newConfig: Configuration) {
+ // Run the default commands.
+ super.onConfigurationChanged(newConfig)
+
+ // Get the current page.
+ val currentPage = webViewViewPager2.currentItem
+
+ // Toggle the pages if there is more than one so that the view pager will recalculate their size.
+ if (currentPage > 0) {
+ // Switch to the previous page.
+ webViewViewPager2.currentItem = (currentPage - 1)
+
+ // Switch back to the current page after the view pager has quiesced.
+ webViewViewPager2.post { webViewViewPager2.currentItem = currentPage }
+ }
+ }
+
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu. This adds items to the app bar if it is present.
menuInflater.inflate(R.menu.webview_options_menu, menu)
// Set the first page as the current WebView.
setCurrentWebView(0)
} else { // The first tab is not selected.
- // Move to the selected tab.
- webViewViewPager2.currentItem = savedTabPosition
+ // Create a handler move to the page.
+ val setCurrentPageHandler = Handler(Looper.getMainLooper())
+
+ // Create a runnable to move to the page.
+ val setCurrentPageRunnable = Runnable {
+ // Move to the page.
+ webViewViewPager2.currentItem = savedTabPosition
+ }
+
+ // Move to the page after 50 milliseconds, which should be enough time to for the WebView state adapter to populate the restored pages.
+ setCurrentPageHandler.postDelayed(setCurrentPageRunnable, 50)
}
// Get the intent that started the app.