From b4d08a67336094fb834c3e37db9b509dd54e771c Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Sun, 24 Oct 2021 12:17:54 +0200 Subject: [PATCH] Improve behavior of Android's back button in file browser (#1006) Co-authored-by: qwerty287 Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1006 Reviewed-by: M M Arif Co-authored-by: qwerty287 Co-committed-by: qwerty287 --- .../mian/gitnex/fragments/FilesFragment.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/src/main/java/org/mian/gitnex/fragments/FilesFragment.java b/app/src/main/java/org/mian/gitnex/fragments/FilesFragment.java index 1f7c0ec4..7e8bfa6d 100644 --- a/app/src/main/java/org/mian/gitnex/fragments/FilesFragment.java +++ b/app/src/main/java/org/mian/gitnex/fragments/FilesFragment.java @@ -11,6 +11,7 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.EditorInfo; +import androidx.activity.OnBackPressedCallback; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; @@ -121,6 +122,24 @@ public class FilesFragment extends Fragment implements FilesAdapter.FilesAdapter }); + requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) { + + @Override + public void handleOnBackPressed() { + if(path.size() == 0 || RepoDetailActivity.mViewPager.getCurrentItem() != 1) { + requireActivity().finish(); + return; + } + path.remove(path.size() - 1); + binding.breadcrumbsView.removeLastItem(); + if(path.size() == 0) { + fetchDataAsync(Authorization.get(getContext()), repoOwner, repoName, ref); + } else { + fetchDataAsyncSub(Authorization.get(getContext()), repoOwner, repoName, path.toString(), ref); + } + } + }); + ((RepoDetailActivity) requireActivity()).setFragmentRefreshListenerFiles(repoBranch -> { path.clear();