Screen rotation while viewing media details in SearchActivity forcibly navigates back to search results
Summary
When a user searches for media in SearchActivity and opens a search result to view its full details (MediaDetailPagerFragment), rotating the screen causes the app to forcibly close the media viewer and return to the search list.
Steps to Reproduce
- Open the app and tap the Search icon.
- Search for any term (e.g. "flower", "cat").
- Tap on any media item to open the full-screen media details viewer.
- Rotate the device (change orientation from portrait to landscape or vice versa).
Expected Behavior
The media details viewer should remain open on the selected media item, adjusting its layout for the new orientation.
Actual Behavior
The media viewer is dismissed automatically, throwing the user back to the search results list. Any zoom or active inspection state is lost.
Root Cause
In app/src/main/java/fr/free/nrw/commons/explore/SearchActivity.kt lines 238–249, there is a temporary workaround implemented in onResume():
override fun onResume() {
if (supportFragmentManager!!.backStackEntryCount == 1) {
// FIXME: Temporary fix for screen rotation inside media details. If we don't call onBackPressed then
// fragment stack is increasing every time.
// FIXME: Similar issue like this https://github.com/commons-app/apps-android-commons/issues/894
// This is called on screen rotation when user is inside media details. Ideally it should show Media
// Details but since we are not saving the state now. We are throwing the user to search screen otherwise
// the app was crashing.
onBackPressedDispatcher.onBackPressed()
}
super.onResume()
}
Because fragment and selection states were not retained across configuration changes, the code deliberately triggers onBackPressed() on every orientation change while viewing media details.
Proposed Solution
- Save the active media position or fragment state in
onSaveInstanceStateand restore it inonRestoreInstanceState/onCreate. - Check if the fragment transaction can be retained across orientation changes rather than re-creating duplicate transactions in the back stack.
- Remove the
onBackPressedDispatcher.onBackPressed()workaround fromonResume(). - Add unit / instrumentation tests covering orientation changes in
SearchActivity.
I tried reproducing this, and it works fine for me. Can you attach a screencast showing the same?
https://github.com/user-attachments/assets/7e6202ac-aee1-4be1-8453-6a2e17d446cb