后退按钮以退出MVVM中的Zxing扫描仪

2022-08-27 00:00:00 android back back-button mvvm zxing

我一直在尝试使用ZXING库,但无法使其正常工作。

我创建了布局XML、视图模型和片段,因为我的项目是MVVM模式。 最初,它显示的是我的布局视图。但问题是,Zxing在库中有自己的屏幕,可以用它扫描代码。 因此,当按下Back按钮时,我无法返回,但它会关闭自己的屏幕,然后显示我的布局。因此,我必须按两次后退按钮才能退出zxing扫描仪和版面视图。

我们将非常感谢您的帮助。

我尝试的内容

  1. 我设置了On BackPressed以支持两个堆栈。但在ZXING扫描仪之后它仍然显示我的布局。
  2. 我尝试对我的布局XML调用扫描器,但由于同样的原因未成功。

我想做什么 按一次后退按钮可退出QR扫描仪屏幕。

片段

class QrScanFragment  : Fragment() {

private lateinit var binding: FragmentQrScanBinding
private lateinit var viewModel: QrScanViewModel
private lateinit var mContext: MainActivity
private lateinit var callback : OnBackPressedCallback

override fun onAttach(context: Context) {
    super.onAttach(context)
    mContext = context as MainActivity
    callback = object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            Timber.d("onBackPressed")
            findNavController().popBackStack()
        }
    }
    requireActivity().onBackPressedDispatcher.addCallback(this, callback)
}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {
    binding = DataBindingUtil.inflate(inflater, R.layout.fragment_qr_scan, container, false)
    return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    viewModel = ViewModelProvider(this).get(QrScanViewModel::class.java)
    binding.viewModel = viewModel
    binding.lifecycleOwner = viewLifecycleOwner

    val integrator = IntentIntegrator.forSupportFragment(this@QrScanFragment)
    integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE)
    integrator.setPrompt("Scan code")
    integrator.setCameraId(0)
    integrator.setBeepEnabled(false)
    integrator.setBarcodeImageEnabled(true)
    integrator.initiateScan()

}

override fun onDetach() {
    super.onDetach()
    callback.remove()
}
}

XML

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
    <variable
        name="viewModel"
        type="com.project.view.qrScan.QrScanViewModel" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

viewModel

class QrScanViewModel: ViewModel(){}

解决方案

使用onActivityResult获取回调。从该回调中,您可以取消您的视图

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if(result != null) {
        if(result.getContents() == null) {
            //Dismiss your view if Zxing was cancelled
        } else {
            //Zxing returned with result, you could should the result or dismiss your view
        }
    } else {
        // This is important, otherwise the result will not be passed to the fragment
        super.onActivityResult(requestCode, resultCode, data);
    }
}

相关文章