diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index a080699d4..5155041bc 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -26,12 +26,15 @@ import java.util.Arrays; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.view.ActionMode; import androidx.appcompat.widget.SearchView; import androidx.appcompat.widget.Toolbar; import androidx.core.splashscreen.SplashScreen; import androidx.recyclerview.widget.RecyclerView; + import protect.card_locker.preferences.SettingsActivity; public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCardCursorAdapter.CardAdapterListener, GestureDetector.OnGestureListener { @@ -52,6 +55,9 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard private View mNoMatchingCardsText; private View mNoGroupCardsText; + private ActivityResultLauncher mMainRequestLauncher; + private ActivityResultLauncher mBarcodeScannerLauncher; + private ActionMode.Callback mCurrentActionModeCallback = new ActionMode.Callback() { @Override public boolean onCreateActionMode(ActionMode inputMode, Menu inputMenu) { @@ -260,6 +266,34 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard .show(); } */ + + mMainRequestLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> { + // We're coming back from another view so clear the search + // We only do this now to prevent a flash of all entries right after picking one + mFilter = ""; + if (mMenu != null) { + MenuItem searchItem = mMenu.findItem(R.id.action_search); + searchItem.collapseActionView(); + } + }); + + mBarcodeScannerLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> { + Intent intent = result.getData(); + BarcodeValues barcodeValues = Utils.parseSetBarcodeActivityResult(Utils.BARCODE_SCAN, result.getResultCode(), intent, this); + + if (!barcodeValues.isEmpty()) { + Intent newIntent = new Intent(getApplicationContext(), LoyaltyCardEditActivity.class); + Bundle newBundle = new Bundle(); + newBundle.putString(LoyaltyCardEditActivity.BUNDLE_BARCODETYPE, barcodeValues.format()); + newBundle.putString(LoyaltyCardEditActivity.BUNDLE_CARDID, barcodeValues.content()); + Bundle inputBundle = intent.getExtras(); + if (inputBundle != null && inputBundle.getString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP) != null) { + newBundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, inputBundle.getString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP)); + } + newIntent.putExtras(newBundle); + startActivity(newIntent); + } + }); } @Override @@ -320,44 +354,11 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard bundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, groupsTabLayout.getTabAt(selectedTab).getText().toString()); } intent.putExtras(bundle); - startActivityForResult(intent, Utils.BARCODE_SCAN); + mBarcodeScannerLauncher.launch(intent); }); addButton.bringToFront(); } - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent intent) { - super.onActivityResult(requestCode, resultCode, intent); - - if (requestCode == Utils.MAIN_REQUEST) { - // We're coming back from another view so clear the search - // We only do this now to prevent a flash of all entries right after picking one - mFilter = ""; - if (mMenu != null) { - MenuItem searchItem = mMenu.findItem(R.id.action_search); - searchItem.collapseActionView(); - } - updateLoyaltyCardList(); - - return; - } - - BarcodeValues barcodeValues = Utils.parseSetBarcodeActivityResult(requestCode, resultCode, intent, this); - - if (!barcodeValues.isEmpty()) { - Intent newIntent = new Intent(getApplicationContext(), LoyaltyCardEditActivity.class); - Bundle newBundle = new Bundle(); - newBundle.putString(LoyaltyCardEditActivity.BUNDLE_BARCODETYPE, barcodeValues.format()); - newBundle.putString(LoyaltyCardEditActivity.BUNDLE_CARDID, barcodeValues.content()); - Bundle inputBundle = intent.getExtras(); - if (inputBundle != null && inputBundle.getString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP) != null) { - newBundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, inputBundle.getString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP)); - } - newIntent.putExtras(newBundle); - startActivity(newIntent); - } - } - @Override public void onBackPressed() { if (mMenu != null) { @@ -542,25 +543,25 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard if (id == R.id.action_manage_groups) { Intent i = new Intent(getApplicationContext(), ManageGroupsActivity.class); - startActivityForResult(i, Utils.MAIN_REQUEST); + mMainRequestLauncher.launch(i); return true; } if (id == R.id.action_import_export) { Intent i = new Intent(getApplicationContext(), ImportExportActivity.class); - startActivityForResult(i, Utils.MAIN_REQUEST); + mMainRequestLauncher.launch(i); return true; } if (id == R.id.action_settings) { Intent i = new Intent(getApplicationContext(), SettingsActivity.class); - startActivityForResult(i, Utils.MAIN_REQUEST); + mMainRequestLauncher.launch(i); return true; } if (id == R.id.action_about) { Intent i = new Intent(getApplicationContext(), AboutActivity.class); - startActivityForResult(i, Utils.MAIN_REQUEST); + mMainRequestLauncher.launch(i); return true; } @@ -728,7 +729,7 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard ShortcutHelper.updateShortcuts(MainActivity.this, loyaltyCard); - startActivityForResult(i, Utils.MAIN_REQUEST); + mMainRequestLauncher.launch(i); } } }