【问题标题】:Android Activity destroyed when calling ZXing barcode scanner. How to recover?调用 ZXing 条码扫描器时,Android Activity 被破坏。如何恢复?
【发布时间】:2016-08-30 13:50:31
【问题描述】:

首先,我使用的是 Xamarin 和 MvvmCross。

在我的 ViewModel 中,我使用 ZXing MobileBarcodeScanner 类在用户单击按钮时扫描条形码:

var scanner = new MobileBarcodeScanner();
var result = await scanner.Scan();
if (result != null)
{
    CodigoProduto = result.Text;
    InternalPesquisarProduto();
}

扫描后,我运行InternalPesquisarProduto void,它在远程服务器上搜索数据,当然,基于读取的条形码。该方法在获取数据时也会显示一些加载消息:

Ui.DisplayLoading("Searching...", "Searching data");
// Code that fetches the data
Ui.DismissLoading();

Ui 是我的 ViewModel 上的一个属性,定义如下:

protected IUiInteractor Ui { get; set; }

我通过依赖注入接收它。以下是此场景中使用的实现的相关代码:

public class AndroidUiInteractor : IUiInteractor
{
    private IMvxAndroidCurrentTopActivity _mvxCurrentTopActivity;

    public AndroidUiInteractor(IMvxAndroidCurrentTopActivity mvxCurrentTopActivity)
    {
        _mvxCurrentTopActivity = mvxCurrentTopActivity;
    }

    public void DisplayLoading(string title, string message)
    {
        _mvxCurrentTopActivity.Activity.RunOnUiThread(() =>
        {
            _progressDlg = new ProgressDialog(_mvxCurrentTopActivity.Activity);
            // Configuring the title and the message
            _progressDlg.Show();
        });
    }
}

问题是当scanner.Scan被调用时,我的调用者活动被破坏了,所以当我调用Ui.DisplayLoading时,_mvxCurrentTopActivity.Activitynull

这个案例最奇怪的是,我在测试中使用了两个带有 Android 5.0 API 21 的三星,而这个问题只发生在其中一个中,另一方面,调用时活动没有被破坏scanner.Scan

注意:对于代码中的任何错误,我很抱歉,但由于公司政策,我只能通过终端服务访问互联网,并且 Ctrl + V 已禁用。

【问题讨论】:

  • 在调用InternalPesquisarProduto 之前为什么不先返回到上一个Activity?

标签: c# android xamarin mvvmcross


【解决方案1】:

原来问题出在设备上。重置它的配置后它工作正常。

对于面临该问题的每个人来说,这可能不是一个最终的解决方案,但在我的场景中是可以做到的。

【讨论】:

    猜你喜欢
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多