From 05eb0d0fbe7ffc13d6b9a431d0825c6ce6c8dc79 Mon Sep 17 00:00:00 2001 From: XiangRongLin <41164160+XiangRongLin@users.noreply.github.com> Date: Mon, 31 May 2021 12:40:14 +0200 Subject: [PATCH] Commit path immediately when import backup --- .../settings/ContentSettingsFragment.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java index c77ca034f18..7b2b1aeb5d4 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java @@ -1,5 +1,6 @@ package org.schabi.newpipe.settings; +import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.Intent; @@ -205,7 +206,7 @@ private void exportDatabase(final File folder) { .getDefaultSharedPreferences(requireContext()); manager.exportDatabase(preferences, path); - setImportExportDataPath(folder); + setImportExportDataPath(folder, false); Toast.makeText(getContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT).show(); } catch (final Exception e) { @@ -230,7 +231,7 @@ private void importDatabase(final File file) { if (!manager.extractDb(filePath)) { Toast.makeText(getContext(), R.string.could_not_import_all_files, Toast.LENGTH_LONG) - .show(); + .show(); } //If settings file exist, ask if it should be imported. @@ -264,14 +265,16 @@ private void importDatabase(final File file) { */ private void finishImport(final File file) { if (file.getParentFile() != null) { - setImportExportDataPath(file.getParentFile()); + //immediately because app is about to exit + setImportExportDataPath(file.getParentFile(), true); } // restart app to properly load db System.exit(0); } - private void setImportExportDataPath(final File file) { + @SuppressLint("ApplySharedPref") + private void setImportExportDataPath(final File file, final boolean immediately) { final String directoryPath; if (file.isDirectory()) { directoryPath = file.getAbsolutePath(); @@ -283,6 +286,13 @@ private void setImportExportDataPath(final File file) { directoryPath = ""; } } - defaultPreferences.edit().putString(importExportDataPathKey, directoryPath).apply(); + final SharedPreferences.Editor editor = defaultPreferences + .edit() + .putString(importExportDataPathKey, directoryPath); + if (immediately) { + editor.commit(); + } else { + editor.apply(); + } } }