英文:
After calling zxing scanner, I go back to my activity but in a new instance
问题
我在我的Android应用中使用了库zxing-android-embedded。在从我的活动调用initiateScan()方法启动扫描仪之前,我设置了一个类变量scanedItemId,以便知道我点击了哪个项目进行扫描。
我的问题是,当扫描仪活动完成时,它会返回到我的活动,但是是一个新的实例,而不是最初的实例(我在onCreate方法中使用断点进行了检查)。因此,我的类变量为null。我该怎么做才能保持我的初始活动实例处于活动状态,并确保扫描仪返回到它?
public class MyActivity
[...]
scanedItemId = currentItem.id // 我们点击的当前项目。
IntentIntegrator qrCodeScanner = new IntentIntegrator(this);
qrCodeScanner.setOrientationLocked(false);
qrCodeScanner.initiateScan();
[...]
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE:
// 这里的scanedItemId始终为null。
[...]
英文:
I use the library zxing-android-embedded in my Android App. Before calling initiateScan() method to start the scanner from my activity, I set a class variable scanedItemId to know on which item I clicked to scan.
My issue is that when the scanner activity finished, it goes back to my activity, but in a new instance and not the initial one (I checked with a break point in the onCreate method). So, my class variable is null. What can I do to keep my initial activity instance live and be sure the scanner goes back to it?
public class MyActivity
[...]
scanedItemId = currentItem.id // The current item where we clicked on.
IntentIntegrator qrCodeScanner = new IntentIntegrator(this);
qrCodeScanner.setOrientationLocked(false);
qrCodeScanner.initiateScan();
[...]
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.
[...]
```
</details>
# 答案1
**得分**: 2
您问道:
> 我该怎么做才能保持我的初始活动实例处于活动状态,并确保扫描仪返回到它?
您无法做到这一点。当您的应用程序转入后台且扫描仪应用程序需要资源时,安卓系统会终止您的应用程序,以便将资源释放给扫描仪应用程序。这是正常情况,您的应用程序需要足够强大,以处理这种情况。如果您需要跟踪您的应用程序状态,以便在应用程序返回前台时能够继续运行,那么您需要将该信息保存在某个持久位置。您有以下选择:
- SharedPreferences
- SQLite 数据库
- 使用文件
- 实现 `onSaveInstanceState()`
<details>
<summary>英文:</summary>
You asked:
> What can I do to keep my initial activity instance live and be sure
> the scanner goes back to it?
You can't. When your app goes to the background and the scanner app needs resources, Android will kill your app to make the resources available to the scanner app. This is normal and your app needs to be made robust enough to deal with this. If you need to keep track of your app's state so that you can continue when your app returns to the foreground, then you need to save that information somewhere persistent. You have choices:
- SharedPreferences
- SQLite database
- Use a file
- Implement `onSaveInstanceState()`
</details>
# 答案2
**得分**: 1
根据David说的,我实现了"onSaveInstanceState()"解决方案,它运行得很好:我只是在我的活动中添加了这个:
```java
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("scanedItemIdKey", scanedItemId);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
scanedItemId = savedInstanceState.getInt("scanedItemIdKey");
}
```
谢谢。
<details>
<summary>英文:</summary>
Following what @David said, I implemented the "onSaveInstanceState()" solution which works fine: I just added this in my activity:
```
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("scanedItemIdKey", scanedItemId);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
scanedItemId = savedInstanceState.getInt("scanedItemIdKey");
}
```
Thanks
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论