r/JetpackCompose • u/Exact-Ad-9606 • Nov 01 '26
HorizontalPager opened on the wrong page after restarts: the cause was touch mode
I hit a confusing bug while building a launcher in Compose: my HorizontalPager kept opening on page 1 instead of page 0, even after a force-stop and with no saved state.
Logging showed the pager starting on page 0, then moving to page 1 with no touch. Here's what was happening:
- Key input, such as a keyboard, a D-pad or `adb shell input text`, puts the system in non-touch mode.
- In that mode, Android looks for something to focus when the window opens.
- With beyondViewportPageCount = 1, the next page was composed off screen, so it could get the focus. The pager then scrolled to bring it into view.
- Touch mode belongs to the system rather than the app, so it survived restarts until the next touch.
Setting beyondViewportPageCount = 0 fixed it, since off-screen pages are no longer composed. The trade-off is that the next page is composed as you swipe, instead of being ready in advance.
I tested the fix with simulated key input on one phone, not with a physical keyboard or remote. If you've handled this differently, I'd like to hear how, for example keeping the off-screen page composed while stopping it from taking focus.
The project is open source if you want to see the code: https://github.com/MedTahiri/ai-launcher-android