Skip to content

Commit

Permalink
Replace use of onBackPressed
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpoole committed Aug 3, 2024
1 parent 8cee056 commit 2288407
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/de/blau/android/HelpViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public boolean onOptionsItemSelected(final MenuItem item) {
webView.goBack();
invalidateOptionsMenu();
} else {
onBackPressed(); // return to caller
getOnBackPressedDispatcher().onBackPressed(); // return to caller
}
return true;

Expand Down
26 changes: 20 additions & 6 deletions src/main/java/de/blau/android/filter/TagFilterActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.blau.android.filter;

import static de.blau.android.contract.Constants.LOG_TAG_LEN;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import android.content.ContentValues;
Expand All @@ -25,6 +27,7 @@
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
Expand All @@ -43,7 +46,8 @@
*/
public class TagFilterActivity extends ListActivity {

private static final String DEBUG_TAG = TagFilterActivity.class.getSimpleName().substring(0, Math.min(23, TagFilterActivity.class.getSimpleName().length()));
private static final int TAG_LEN = Math.min(LOG_TAG_LEN, TagFilterActivity.class.getSimpleName().length());
private static final String DEBUG_TAG = TagFilterActivity.class.getSimpleName().substring(0, TAG_LEN);
private static final String FILTER_KEY = "FILTER";

static final String FILTERENTRIES_TABLE = "filterentries";
Expand Down Expand Up @@ -127,6 +131,8 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
getListView().setItemsCanFocus(true);
// Attach cursor adapter to the ListView
getListView().setAdapter(filterAdapter);

getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback);
}

@Override
Expand Down Expand Up @@ -230,11 +236,19 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
updateDatabaseFromList();
super.onBackPressed();
}
/**
* Update database if exiting via back button
*/
private OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(true) {

@Override
public void handleOnBackPressed() {
Log.d(DEBUG_TAG, "onBackPressed()");
updateDatabaseFromList();
onBackPressedCallback.setEnabled(false);
getOnBackPressedDispatcher().onBackPressed();
}
};

/**
* Update the database from the whole view
Expand Down

0 comments on commit 2288407

Please sign in to comment.