Android:将数据从初始 activity(启动 Zxing 扫描器)传递到 onActivityResult 回调
Android: pass data from initial activity (launching Zxing scanner) to onActivityResult callback
我在我的 Android 应用程序中使用了库 zxing-android-embedded。在调用 initiateScan() 方法从我的 activity 启动扫描仪之前,我设置了一个 class 变量 scanedItemId 以了解我单击扫描的项目:
[...]
scanedItemId = currentItem.id // The current item where we clicked on.
IntentIntegrator qrCodeScanner = new IntentIntegrator(MyActivity.this);
qrCodeScanner.setOrientationLocked(false);
qrCodeScanner.initiateScan();
[...]
然后我通过 onActivityResult 方法得到结果。它运行良好,但我的 class 变量 scanedItemId 为空,因为 activity 已重新启动。是否可以保留我的 MyActivity 初始实例(正确设置 scanedItemId)或通过 IntentIntegrator 传递我需要的值以在 MyActivity 的新实例上取回它?
[...]
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE:
// Here the scanedItemId is always null.
[...]
如果可能的话,我不想使用硬持久性(如数据库或文件)来获取我的 scanedItemId 值。
希望够清楚
您可以像这样添加更多自定义数据:
scanedItemId = currentItem.id // The current item where we clicked on.
IntentIntegrator qrCodeScanner = new IntentIntegrator(MyActivity.this);
quCodeScanner.addExtra("ITEM_ID", scanedItemId);//add your data
qrCodeScanner.setOrientationLocked(false);
qrCodeScanner.initiateScan();
因为您使用的图书馆没有return您的数据。您可以分叉该库并添加如下代码:
在 CaptureManager 中 class
编辑 initializeFromIntent 方法
if (intent != null) {
data = intent.getIntExtra("ITEM_ID", 0);//save your data
}
和 resultIntent 方法
intent.putExtra("ITEM_ID", data);//return your data
现在您可以在 onActivityResult 中获取您的数据
IntentResult result = IntentIntegrator.parseActivityResult(resultCode, data);
if (result.getOriginalIntent().hasExtra("ITEM_ID")) {
Toast.makeText(this,"Item Id : " + String.valueOf(result.getOriginalIntent().getIntExtra("ITEM_ID",0)), Toast.LENGTH_LONG).show();
}
我在我的 Android 应用程序中使用了库 zxing-android-embedded。在调用 initiateScan() 方法从我的 activity 启动扫描仪之前,我设置了一个 class 变量 scanedItemId 以了解我单击扫描的项目:
[...]
scanedItemId = currentItem.id // The current item where we clicked on.
IntentIntegrator qrCodeScanner = new IntentIntegrator(MyActivity.this);
qrCodeScanner.setOrientationLocked(false);
qrCodeScanner.initiateScan();
[...]
然后我通过 onActivityResult 方法得到结果。它运行良好,但我的 class 变量 scanedItemId 为空,因为 activity 已重新启动。是否可以保留我的 MyActivity 初始实例(正确设置 scanedItemId)或通过 IntentIntegrator 传递我需要的值以在 MyActivity 的新实例上取回它?
[...]
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE:
// Here the scanedItemId is always null.
[...]
如果可能的话,我不想使用硬持久性(如数据库或文件)来获取我的 scanedItemId 值。
希望够清楚
您可以像这样添加更多自定义数据:
scanedItemId = currentItem.id // The current item where we clicked on.
IntentIntegrator qrCodeScanner = new IntentIntegrator(MyActivity.this);
quCodeScanner.addExtra("ITEM_ID", scanedItemId);//add your data
qrCodeScanner.setOrientationLocked(false);
qrCodeScanner.initiateScan();
因为您使用的图书馆没有return您的数据。您可以分叉该库并添加如下代码:
在 CaptureManager 中 class 编辑 initializeFromIntent 方法
if (intent != null) {
data = intent.getIntExtra("ITEM_ID", 0);//save your data
}
和 resultIntent 方法
intent.putExtra("ITEM_ID", data);//return your data
现在您可以在 onActivityResult 中获取您的数据
IntentResult result = IntentIntegrator.parseActivityResult(resultCode, data);
if (result.getOriginalIntent().hasExtra("ITEM_ID")) {
Toast.makeText(this,"Item Id : " + String.valueOf(result.getOriginalIntent().getIntExtra("ITEM_ID",0)), Toast.LENGTH_LONG).show();
}