diff --git a/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index 3e14f01852df..d3846fe4e136 100644 --- a/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -233,6 +233,22 @@ OCFile getFileById(long id) { return null; } + public OCFile getNthParent(OCFile file, int n) { + if (file == null || !file.isFolder() || n < 1) { + return null; + } + + OCFile currentFile = file; + for (int i = 0; i < n; i++) { + currentFile = getFileById(currentFile.getParentId()); + if (currentFile == null) { + return null; + } + } + + return currentFile; + } + public @Nullable OCFile getFileByLocalPath(String path) { FileEntity fileEntity = fileDao.getFileByLocalPath(path, user.getAccountName()); diff --git a/app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java b/app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java index 0abe87cfe371..241310d2ebff 100644 --- a/app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -1029,13 +1029,9 @@ public void onBackPressed() { } else { browseUp(listOfFiles); - if (parentDir != null) { - OCFile grandParentDir = fileDataStorageManager.getFileById(parentDir.getParentId()); - if (grandParentDir != null) { - if (grandParentDir.isRoot()) { - filterCurrentDirectory(); - } - } + OCFile secondParentDir = fileDataStorageManager.getNthParent(currentDir, 2); + if (secondParentDir != null && secondParentDir.isRoot()) { + filterCurrentDirectory(); } } } else {