Skip to content

fix: drawer category views reset to Home directory after rotation - #4676

Open
thirumani-vihaan wants to merge 2 commits into
TeamAmaze:release/4.0from
thirumani-vihaan:fix/4629-drawer-rotation
Open

fix: drawer category views reset to Home directory after rotation#4676
thirumani-vihaan wants to merge 2 commits into
TeamAmaze:release/4.0from
thirumani-vihaan:fix/4629-drawer-rotation

Conversation

@thirumani-vihaan

@thirumani-vihaan thirumani-vihaan commented Jul 14, 2026

Copy link
Copy Markdown

Description

When the device is rotated, the FragmentStateAdapter aggressively recreates TabFragment, which subsequently calls updatePaths(). In non-FILE modes (like OpenMode.CUSTOM for Images or OpenMode.RECENT for Recent Files), this inadvertently overwrote the database with the Home directory. This PR explicitly overrides onSaveInstanceState in MainFragment to preserve the currentPath and openMode, ensuring the state is correctly restored when the ViewModel is re-initialized post-rotation.

Issue tracker

Fixes #4629

Automatic tests

  • Added unit tests for specific individual functions
  • Added headless tests for new app functionality
  • Added emulator tests for new UI elements

Manual tests

  • Done

  • Device: Pixel Emulator

  • OS: Android 14 (API 34)

Build tasks success

Successfully running following tasks on local:

  • ./gradlew assembledebug
  • ./gradlew spotlessCheck

Generative code

  • This PR used generative code tools (GenAI, LLMs, etc.)

  • Model: Gemini 3.1

  • Version: 3.1

  • Provider: Google DeepMind

Fixes: TeamAmaze#4629

The issue occurs when a user navigates to a Drawer Category (like "Images") and rotates the device. After rotation, the view incorrectly resets to the default Home directory.

This happens because the MainFragmentViewModel's currentPath is either lost or re-initialized from the original fragment arguments (which contain the Home path) upon recreation. To fix this, I implemented state-saving logic in MainFragment using onSaveInstanceState to explicitly preserve the currentPath and openMode, and restore them in onCreate().
@thirumani-vihaan
thirumani-vihaan changed the base branch from master to release/4.0 July 14, 2026 15:25
@thirumani-vihaan
thirumani-vihaan force-pushed the fix/4629-drawer-rotation branch from 0c9445b to d0130b6 Compare July 14, 2026 18:30
@VishnuSanal VishnuSanal added the PR-Awaiting-Initial-Review this PR is awaiting for an initial review label Jul 19, 2026
sharedPref = PreferenceManager.getDefaultSharedPreferences(requireActivity());
mainFragmentViewModel.initBundleArguments(getArguments());
if (savedInstanceState != null) {
String savedPath = savedInstanceState.getString(KEY_SAVED_CURRENT_PATH);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider extracting the saved state restoration into a separate method to keep onCreate() focused and improve readability.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alihrhera , I extracted the state restoration logic into a separate restoreSavedState method to clean up onCreate. I also swapped out a bulky try/catch block in onSaveInstanceState for a simple null check while I was at it.

Could you please take another look and see if everything looks good?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I suggest using early returns instead of nested if statements to improve readability. For example:

private void restoreSavedState(Bundle savedInstanceState) {
    if (savedInstanceState == null) {
        return;
    }

    String savedPath = savedInstanceState.getString(KEY_SAVED_CURRENT_PATH);
    if (savedPath != null) {
        mainFragmentViewModel.setCurrentPath(savedPath);
    }

    int savedOpenMode = savedInstanceState.getInt(KEY_SAVED_OPEN_MODE, -1);
    if (savedOpenMode != -1) {
        mainFragmentViewModel.setOpenMode(OpenMode.getOpenMode(savedOpenMode));
    }
}

@VishnuSanal VishnuSanal changed the title Fix/4629 drawer rotation fix: drawer category views reset to Home directory after rotation Jul 27, 2026
@thirumani-vihaan
thirumani-vihaan force-pushed the fix/4629-drawer-rotation branch 2 times, most recently from 8771ef0 to 1904d65 Compare July 27, 2026 19:25
@thirumani-vihaan
thirumani-vihaan force-pushed the fix/4629-drawer-rotation branch from 1904d65 to 1b5877f Compare July 28, 2026 03:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR-Awaiting-Initial-Review this PR is awaiting for an initial review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Drawer category views reset to Home directory after rotation

3 participants