From f3b3cd1faa95a29058544238c596a46b707d0c7d Mon Sep 17 00:00:00 2001 From: Werner Rabl Date: Thu, 10 Sep 2026 12:42:57 +0200 Subject: [PATCH] fix: back button moves up a folder instead of closing app --- .../files/filelist/MainFileListFragment.kt | 23 +++++++++---------- .../ui/activity/FileDisplayActivity.kt | 21 ++++++++++++++++- .../ui/activity/FolderPickerActivity.kt | 17 ++++++++++++++ 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/opencloudApp/src/main/java/eu/opencloud/android/presentation/files/filelist/MainFileListFragment.kt b/opencloudApp/src/main/java/eu/opencloud/android/presentation/files/filelist/MainFileListFragment.kt index 21acbf63bb..7da6e7af27 100644 --- a/opencloudApp/src/main/java/eu/opencloud/android/presentation/files/filelist/MainFileListFragment.kt +++ b/opencloudApp/src/main/java/eu/opencloud/android/presentation/files/filelist/MainFileListFragment.kt @@ -429,7 +429,7 @@ class MainFileListFragment : Fragment(), showOrHideFab(requireArguments().getParcelable(ARG_FILE_LIST_OPTION)!!, requireArguments().getParcelable(ARG_INITIAL_FOLDER_TO_DISPLAY)!!) - setFabMainContentDescription() + setFabMainContentDescription(false) setTextHintRootToolbar() } @@ -1008,6 +1008,7 @@ class MainFileListFragment : Fragment(), (newFileListOption.isSharedByLink() && currentFolder.remotePath != ROOT_PATH) if (!isFabSupportedView || isPickingAFolder() || (!currentFolder.hasAddFilePermission && !currentFolder.hasAddSubdirectoriesPermission)) { + collapseFab() toggleFabVisibility(false) } else { toggleFabVisibility(true) @@ -1057,12 +1058,7 @@ class MainFileListFragment : Fragment(), fabMkdir.isFocusable = isFabExpanded() fabNewfile.isFocusable = isFabExpanded() fabNewshortcut.isFocusable = isFabExpanded() - if (fabMain.isExpanded) { - binding.fabMain.findViewById(com.getbase.floatingactionbutton.R.id.fab_expand_menu_button) - .contentDescription = getString(R.string.content_description_add_new_content_expanded) - } else { - setFabMainContentDescription() - } + setFabMainContentDescription(fabMain.isExpanded) } } } @@ -1120,6 +1116,7 @@ class MainFileListFragment : Fragment(), } fun collapseFab() { + val binding = _binding ?: return binding.apply { fabMain.collapse() fabUpload.isFocusable = false @@ -1128,14 +1125,16 @@ class MainFileListFragment : Fragment(), fabNewfile.isFocusable = false fabNewshortcut.isFocusable = false } - + setFabMainContentDescription(false) } - fun isFabExpanded() = binding.fabMain.isExpanded + fun isFabExpanded() = binding.fabMain.isVisible && binding.fabMain.isExpanded - fun setFabMainContentDescription() { - binding.fabMain.findViewById(com.getbase.floatingactionbutton.R.id.fab_expand_menu_button).contentDescription = - getString(R.string.content_description_add_new_content) + fun setFabMainContentDescription(expanded: Boolean) { + val resId = if (expanded) {R.string.content_description_add_new_content_expanded } else { R.string.content_description_add_new_content } + binding.fabMain.findViewById( + com.getbase.floatingactionbutton.R.id.fab_expand_menu_button + ).contentDescription = getString(resId) } private fun openBottomSheetToUploadFiles() { diff --git a/opencloudApp/src/main/java/eu/opencloud/android/ui/activity/FileDisplayActivity.kt b/opencloudApp/src/main/java/eu/opencloud/android/ui/activity/FileDisplayActivity.kt index 9515048ae1..ff34a90a84 100644 --- a/opencloudApp/src/main/java/eu/opencloud/android/ui/activity/FileDisplayActivity.kt +++ b/opencloudApp/src/main/java/eu/opencloud/android/ui/activity/FileDisplayActivity.kt @@ -50,6 +50,7 @@ import android.widget.FrameLayout import android.widget.LinearLayout import android.widget.ScrollView import android.widget.TextView +import androidx.activity.OnBackPressedCallback import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AlertDialog import androidx.core.content.ContextCompat @@ -202,6 +203,17 @@ class FileDisplayActivity : FileActivity(), private var isLightUser = false private var isMultiPersonal = false + private val onBackPressedCallback = object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + handleBackPressedInternal() + } + } + + private fun addOnBackPressedCallback() { + onBackPressedCallback.remove() + onBackPressedDispatcher.addCallback(this, onBackPressedCallback) + } + override fun onCreate(savedInstanceState: Bundle?) { Timber.v("onCreate() start") @@ -307,6 +319,8 @@ class FileDisplayActivity : FileActivity(), } } + addOnBackPressedCallback() + Timber.v("onCreate() end") } @@ -536,6 +550,7 @@ class FileDisplayActivity : FileActivity(), transaction.replace(R.id.right_fragment_container, fragment, TAG_SECOND_FRAGMENT) transaction.commitNow() updateFragmentsVisibility(true) + mainFileListFragment?.collapseFab() } private fun showBottomNavBar(show: Boolean) { @@ -747,7 +762,12 @@ class FileDisplayActivity : FileActivity(), fileOperationsViewModel.performOperation(copyOperation) } + // This might not be directly called on some devices, see also onBackPressedCallback. override fun onBackPressed() { + handleBackPressedInternal() + } + + private fun handleBackPressedInternal() { val isFabOpen = mainFileListFragment?.isFabExpanded() ?: false /* @@ -765,7 +785,6 @@ class FileDisplayActivity : FileActivity(), } else if (!isDrawerOpen() && isFabOpen) { // close fab mainFileListFragment?.collapseFab() - mainFileListFragment?.setFabMainContentDescription() } else { // Every single menu is collapsed. We can navigate up. if (secondFragment != null) { diff --git a/opencloudApp/src/main/java/eu/opencloud/android/ui/activity/FolderPickerActivity.kt b/opencloudApp/src/main/java/eu/opencloud/android/ui/activity/FolderPickerActivity.kt index fa244ca3e6..bda518094c 100644 --- a/opencloudApp/src/main/java/eu/opencloud/android/ui/activity/FolderPickerActivity.kt +++ b/opencloudApp/src/main/java/eu/opencloud/android/ui/activity/FolderPickerActivity.kt @@ -29,6 +29,7 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.view.View +import androidx.activity.OnBackPressedCallback import androidx.annotation.StringRes import androidx.core.view.isVisible import androidx.core.view.updatePadding @@ -56,7 +57,16 @@ open class FolderPickerActivity : FileActivity(), private lateinit var binding: FilesFolderPickerBinding + private val onBackPressedCallback = object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + handleBackPressedInternal() + } + } + private fun addOnBackPressedCallback() { + onBackPressedCallback.remove() + onBackPressedDispatcher.addCallback(this, onBackPressedCallback) + } override fun onCreate(savedInstanceState: Bundle?) { Timber.d("onCreate() start") @@ -134,6 +144,8 @@ open class FolderPickerActivity : FileActivity(), initAndShowListOfFilesFragment() } + addOnBackPressedCallback() + Timber.d("onCreate() end") } @@ -182,7 +194,12 @@ open class FolderPickerActivity : FileActivity(), return super.onOptionsItemSelected(item) } + // This might not be directly called on some devices, see also onBackPressedCallback. override fun onBackPressed() { + handleBackPressedInternal() + } + + private fun handleBackPressedInternal() { val currentDirDisplayed = mainFileListFragment?.getCurrentFile() // If current file is null (we are in the spaces list, for example), close the activity if (currentDirDisplayed == null) {