调用zxing扫描器后,我回到我的活动,但是在一个新的实例中。

huangapple go评论76阅读模式
英文:

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

您问道:
&gt; 我该怎么做才能保持我的初始活动实例处于活动状态,并确保扫描仪返回到它?

您无法做到这一点。当您的应用程序转入后台且扫描仪应用程序需要资源时,安卓系统会终止您的应用程序,以便将资源释放给扫描仪应用程序。这是正常情况,您的应用程序需要足够强大,以处理这种情况。如果您需要跟踪您的应用程序状态,以便在应用程序返回前台时能够继续运行,那么您需要将该信息保存在某个持久位置。您有以下选择:

- SharedPreferences
- SQLite 数据库
- 使用文件
- 实现 `onSaveInstanceState()`

<details>
<summary>英文:</summary>

You asked:
&gt; What can I do to keep my initial activity instance live and be sure
&gt; the scanner goes back to it?

You can&#39;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&#39;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 &quot;onSaveInstanceState()&quot; solution which works fine: I just added this in my activity:
```
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putInt(&quot;scanedItemIdKey&quot;, scanedItemId);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    scanedItemId = savedInstanceState.getInt(&quot;scanedItemIdKey&quot;);
}
```
Thanks

</details>



huangapple
  • 本文由 发表于 2020年7月24日 16:14:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63069565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定