feat: cold start on scanning nfc tag
This commit is contained in:
@@ -4,6 +4,7 @@ import android.Manifest
|
||||
import android.app.PendingIntent
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.nfc.NfcAdapter
|
||||
import android.nfc.Tag
|
||||
import android.os.Build
|
||||
@@ -54,8 +55,12 @@ class MainActivity : AppCompatActivity() {
|
||||
private val nfcPendingIntent: PendingIntent by lazy {
|
||||
PendingIntent.getActivity(
|
||||
this, 0,
|
||||
Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
|
||||
PendingIntent.FLAG_MUTABLE
|
||||
Intent(this, javaClass).apply {
|
||||
addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
|
||||
// Do NOT add FLAG_ACTIVITY_NEW_TASK — this causes
|
||||
// balDontBringExistingBackgroundTaskStackToFg = true
|
||||
},
|
||||
PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
|
||||
)
|
||||
}
|
||||
|
||||
@@ -102,6 +107,8 @@ class MainActivity : AppCompatActivity() {
|
||||
// Initialise NFC adapter (null on devices without NFC — handled gracefully)
|
||||
nfcAdapter = NfcAdapter.getDefaultAdapter(this)
|
||||
|
||||
nfcVm.onNfcIntent(intent)
|
||||
|
||||
enableEdgeToEdge()
|
||||
|
||||
setContent {
|
||||
@@ -137,13 +144,18 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
// Give this activity first pick of all NFC tag intents while foregrounded.
|
||||
// Passing null for techLists and intentFilters means we intercept every tag type.
|
||||
// Restrict to NDEF only — avoids TAG_DISCOVERED BAL blocks for non-NDEF tags
|
||||
val intentFilters = arrayOf(
|
||||
IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED).also { f ->
|
||||
try { f.addDataType("*/*") } catch (_: IntentFilter.MalformedMimeTypeException) {}
|
||||
}
|
||||
)
|
||||
val techLists = arrayOf(arrayOf(android.nfc.tech.Ndef::class.java.name))
|
||||
nfcAdapter?.enableForegroundDispatch(
|
||||
this,
|
||||
nfcPendingIntent,
|
||||
null, // intercept all intent filters
|
||||
null // intercept all tech lists
|
||||
intentFilters,
|
||||
techLists
|
||||
)
|
||||
}
|
||||
|
||||
@@ -174,12 +186,20 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
// NFC tag discovered → hand off to NfcViewModel for reading
|
||||
val tag = intent.getParcelableExtra<Tag>(NfcAdapter.EXTRA_TAG)
|
||||
val tag = intent.getNfcTag()
|
||||
if (tag != null) {
|
||||
nfcVm.onTagDiscovered(tag)
|
||||
}
|
||||
}
|
||||
|
||||
private fun Intent.getNfcTag(): Tag? =
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
getParcelableExtra(NfcAdapter.EXTRA_TAG, Tag::class.java)
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
getParcelableExtra(NfcAdapter.EXTRA_TAG)
|
||||
}
|
||||
|
||||
// ── Permission / battery helpers (unchanged) ──────────────────────────────
|
||||
|
||||
private fun requestNotificationPermissionIfNeeded() {
|
||||
|
||||
Reference in New Issue
Block a user