apps-android-commons icon indicating copy to clipboard operation
apps-android-commons copied to clipboard

Screen rotation while viewing media details in SearchActivity forcibly navigates back to search results

Open himanchalkaushale opened this issue 1 month ago • 1 comments

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

  1. Open the app and tap the Search icon.
  2. Search for any term (e.g. "flower", "cat").
  3. Tap on any media item to open the full-screen media details viewer.
  4. 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

  1. Save the active media position or fragment state in onSaveInstanceState and restore it in onRestoreInstanceState / onCreate.
  2. Check if the fragment transaction can be retained across orientation changes rather than re-creating duplicate transactions in the back stack.
  3. Remove the onBackPressedDispatcher.onBackPressed() workaround from onResume().
  4. Add unit / instrumentation tests covering orientation changes in SearchActivity.

himanchalkaushale avatar Nov 01 '26 09:11 himanchalkaushale

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

AdityaDarke97 avatar Nov 01 '26 09:11 AdityaDarke97