package com.stoutner.privacybrowser.activities;
+import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
// `dismissingSnackbar` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onOptionsItemSelected()`.
public static boolean dismissingSnackbar;
+ // `closeActivityAfterDismissingSnackbar` is used in `onOptionsItemSelected()`, and `onBackPressed()`.
+ private boolean closeActivityAfterDismissingSnackbar;
+
// `context` is used in `onCreate()`, `onOptionsItemSelected()`, and `onAddDomain()`.
private Context context;
// Dismiss the undo delete `SnackBar` if it is shown.
if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
- undoDeleteSnackbar.dismiss();
-
- // Create a `Runnable` to return to the main activity.
- Runnable navigateHomeRunnable = () -> {
- // Go home.
- NavUtils.navigateUpFromSameTask(this);
- };
+ // Set the close flag.
+ closeActivityAfterDismissingSnackbar = true;
- // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
- Handler handler = new Handler();
- handler.postDelayed(navigateHomeRunnable, 300);
+ // Dismiss the snackbar.
+ undoDeleteSnackbar.dismiss();
} else {
// Go home.
NavUtils.navigateUpFromSameTask(this);
} else { // The device is in single-paned mode and `DomainsListFragment` is displayed.
// Dismiss the undo delete `SnackBar` if it is shown.
if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) {
- undoDeleteSnackbar.dismiss();
+ // Set the close flag.
+ closeActivityAfterDismissingSnackbar = true;
- // Create a `Runnable` to return to the main activity.
- Runnable navigateHomeRunnable = () -> {
- // Go home.
- NavUtils.navigateUpFromSameTask(this);
- };
-
- // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
- Handler handler = new Handler();
- handler.postDelayed(navigateHomeRunnable, 300);
+ // Dismiss the snackbar.
+ undoDeleteSnackbar.dismiss();
} else {
// Go home.
NavUtils.navigateUpFromSameTask(this);
// Update the `ListView`.
domainsListView.setAdapter(domainsPendingDeleteCursorAdapter);
+ // Get a handle for the activity.
+ Activity activity = this;
+
// Display a `Snackbar`.
undoDeleteSnackbar = Snackbar.make(domainsListView, R.string.domain_deleted, Snackbar.LENGTH_LONG)
.setAction(R.string.undo, (View v) -> {
// Delete the selected domain.
domainsDatabaseHelper.deleteDomain(databaseIdToDelete);
- // enable `deleteMenuItem` if the system was waiting for a `Snackbar` to be dismissed.
+ // Enable the delete menu item if the system was waiting for a snackbar to be dismissed.
if (dismissingSnackbar) {
// Create a `Runnable` to enable the delete menu item.
Runnable enableDeleteMenuItemRunnable = () -> {
dismissingSnackbar = false;
};
- // Run `enableDeleteMenuItemRunnable` after 100 milliseconds to make sure that the previous domain has been deleted from the database.
+ // Enable the delete menu icon after 100 milliseconds to make sure that the previous domain has been deleted from the database.
Handler handler = new Handler();
handler.postDelayed(enableDeleteMenuItemRunnable, 100);
}
+
+ // Close the activity if back was pressed.
+ if (closeActivityAfterDismissingSnackbar) {
+ // Go home.
+ NavUtils.navigateUpFromSameTask(activity);
+ }
+
break;
}
}
// Dismiss the undo delete SnackBar if it is shown.
if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) {
- undoDeleteSnackbar.dismiss();
-
- // Create a runnable to return to the main activity.
- Runnable navigateHomeRunnable = super::onBackPressed;
+ // Set the close flag.
+ closeActivityAfterDismissingSnackbar = true;
- // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
- Handler handler = new Handler();
- handler.postDelayed(navigateHomeRunnable, 300);
+ // Dismiss the snackbar.
+ undoDeleteSnackbar.dismiss();
} else {
// Pass `onBackPressed()` to the system.
super.onBackPressed();
} else { // The device is in single-paned mode and the domain list fragment is displayed.
// Dismiss the undo delete SnackBar if it is shown.
if ((undoDeleteSnackbar != null) && undoDeleteSnackbar.isShown()) {
- undoDeleteSnackbar.dismiss();
+ // Set the close flag.
+ closeActivityAfterDismissingSnackbar = true;
- // Create a runnable to return to the main activity.
- Runnable navigateHomeRunnable = super::onBackPressed;
-
- // Navigate home after 300 milliseconds to make sure that the previous domain has been deleted from the database.
- Handler handler = new Handler();
- handler.postDelayed(navigateHomeRunnable, 300);
+ // Dismiss the snackbar.
+ undoDeleteSnackbar.dismiss();
} else {
// Pass `onBackPressed()` to the system.
super.onBackPressed();