Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

android - How to navigate to a fragment from inside CameraX's analyze()?

All my CameraX initializations reside in FragmentA, and my goal is to navigate to FragmentB depending on some condition that must be verified inside analyze().

When navigating directly from analyze(), through Logcat I can see that FragmentB is loaded correctly but the UI freezes on the camera preview, and unfreezes only when I navigate back to FragmentA. I discovered that under those circumstances FragmentA doesn't go through the rest of its lifecycle correctly, meaning that onDestroyView() and the other methods are called only when I navigate back to it, before immediately beginning a new lifecycle; this leads to the Executor thread not calling shutdown() when it's required.

I've tried wrapping the navigation call in a runOnUiThread { } but with no success.

class MyAnalyzer(private val fragmentA: FragmentA) : ImageAnalysis.Analyzer {
    override fun analyze(imageProxy: ImageProxy) {
        if (foo) {
            imageProxy.close()
            fragmentA.requireActivity().runOnUiThread {
                fragmentA.findNavController().navigate(R.id.action_fragmentA_to_fragmentB)
            }
        }

        // do other stuff with imageProxy...

        imageProxy.close()
    }
}

As an alternative to all of this madness I would probably need some sort of callback from analyze() to FragmentA, which gets triggered by the required condition and lets FragmentA handle the navigation directly and properly. Can you provide any insight on the matter?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...