/*
- * Copyright © 2016-2022 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2016-2022 Soren Stoutner <soren@stoutner.com>.
*
* This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
*
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
-import android.preference.PreferenceManager;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
import android.view.Menu;
import android.widget.Spinner;
import android.widget.TextView;
+import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.cursoradapter.widget.CursorAdapter;
import androidx.fragment.app.DialogFragment; // The AndroidX dialog fragment must be used or an error is produced on API <=22.
+import androidx.preference.PreferenceManager;
import com.google.android.material.snackbar.Snackbar;
actionBar.setCustomView(R.layout.spinner);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP);
+ // Control what the system back command does.
+ OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(true) {
+ @Override
+ public void handleOnBackPressed() {
+ // Prepare to finish the activity.
+ prepareFinish();
+ }
+ };
+
+ // Register the on back pressed callback.
+ getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback);
+
// Initialize the database handler.
bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this);
// Close the activity if back has been pressed.
if (closeActivityAfterDismissingSnackbar) {
- onBackPressed();
+ finish();
}
}
});
// Run the command that corresponds to the selected menu item.
if (menuItemId == android.R.id.home) { // Go Home. The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
- // Exit the activity.
- onBackPressed();
+ // Prepare to finish the activity.
+ prepareFinish();
} else if (menuItemId == R.id.sort) { // Toggle the sort mode.
// Update the sort by display order tracker.
sortByDisplayOrder = !sortByDisplayOrder;
savedInstanceState.putBoolean(SORT_BY_DISPLAY_ORDER, sortByDisplayOrder);
}
- @Override
- public void onBackPressed() {
+ private void prepareFinish() {
// Check to see if a snackbar is currently displayed. If so, it must be closed before existing so that a pending delete is completed before reloading the list view in the bookmarks activity.
if ((bookmarksDeletedSnackbar != null) && bookmarksDeletedSnackbar.isShown()) { // Close the bookmarks deleted snackbar before going home.
// Set the close flag.
BookmarksActivity.restartFromBookmarksDatabaseViewActivity = true;
// Exit the bookmarks database view activity.
- super.onBackPressed();
+ finish();
}
}
// Run the default commands.
super.onDestroy();
}
-}
\ No newline at end of file
+}