如何使用意图转到活动的片段页面

发布于 2025-01-21 05:00:42 字数 1977 浏览 1 评论 0原文

这是我的活动函数(i)在扫描条形码后无法转到片段页面,我尝试它可以转到活动页面并显示代码成功,但我需要它转到fragment页面。

barcodeDetector.setProcessor(object : Detector.Processor<Barcode> {

        override fun release() {
            Toast.makeText(applicationContext, "Scanner has been closed", Toast.LENGTH_SHORT)
                .show()
        }

        override fun receiveDetections(detections: Detector.Detections<Barcode>) {
            val barcodes = detections.detectedItems
            if (barcodes.size() == 1) {
                scannedValue = barcodes.valueAt(0).rawValue
                runOnUiThread {
                    cameraSource.stop()
                    Toast.makeText(this@InsertStockInActivity, scannedValue, Toast.LENGTH_SHORT).show()
                    val i = Intent(this@InsertStockInActivity, comFragment::class.java)
                    .putExtra("cameraSource", scannedValue)
                    startActivity(i)
                    finish()
                }
            }else
            {
                Toast.makeText(this@InsertStockInActivity, "value- else", Toast.LENGTH_SHORT).show()

            }
        }

    })

这是我的碎片页面。我写错了吗?

 class comFragment : Fragment() {
       private lateinit var binding: FragmentComBinding
       private val nav by lazy { findNavController() }

       private val vm: StockInViewModel by activityViewModels()

       private val formatter = SimpleDateFormat("dd MMMM yyyy '-' hh:mm:ss a", Locale.getDefault())

       override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
       binding = FragmentComBinding.inflate(inflater, container, false)
       //binding.btnScanBarcode.setOnClickListener{ nav.navigate(R.id.insertStockInActivity) }

       val value = requireActivity().intent.getStringExtra("cameraSource")
       binding.edtId.findViewById<EditText>(R.id.value)

       return binding.root
   }

}

This is my Activity function that the startActivity(i) unable go to the Fragment page after scanning a barcode, I have try that it can go to activity page and show the code successful but I need it go to Fragment page.

barcodeDetector.setProcessor(object : Detector.Processor<Barcode> {

        override fun release() {
            Toast.makeText(applicationContext, "Scanner has been closed", Toast.LENGTH_SHORT)
                .show()
        }

        override fun receiveDetections(detections: Detector.Detections<Barcode>) {
            val barcodes = detections.detectedItems
            if (barcodes.size() == 1) {
                scannedValue = barcodes.valueAt(0).rawValue
                runOnUiThread {
                    cameraSource.stop()
                    Toast.makeText(this@InsertStockInActivity, scannedValue, Toast.LENGTH_SHORT).show()
                    val i = Intent(this@InsertStockInActivity, comFragment::class.java)
                    .putExtra("cameraSource", scannedValue)
                    startActivity(i)
                    finish()
                }
            }else
            {
                Toast.makeText(this@InsertStockInActivity, "value- else", Toast.LENGTH_SHORT).show()

            }
        }

    })

This is my Fragment page. do I write anything wrong?

 class comFragment : Fragment() {
       private lateinit var binding: FragmentComBinding
       private val nav by lazy { findNavController() }

       private val vm: StockInViewModel by activityViewModels()

       private val formatter = SimpleDateFormat("dd MMMM yyyy '-' hh:mm:ss a", Locale.getDefault())

       override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
       binding = FragmentComBinding.inflate(inflater, container, false)
       //binding.btnScanBarcode.setOnClickListener{ nav.navigate(R.id.insertStockInActivity) }

       val value = requireActivity().intent.getStringExtra("cameraSource")
       binding.edtId.findViewById<EditText>(R.id.value)

       return binding.root
   }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灯角 2025-01-28 05:00:42

您使用意图导航到活动fragment

您可以:

  1. 使用 fragment Transactions 导航到新的fragment fragment < /代码>在您的当前活动中


  2. 使用意图来导航到新活动,在您的新活动中.com/guide/fragments/transactions“ rel =“ nofollow noreferrer”> fragment Transactions 到您的fragment

You use intent to navigate to Activity not to Fragment.

You can:

  1. Use fragment transactions to navigate to new Fragment in your current Activity

  2. Use intent to navigate to new Activity and in your new Activity use fragment transactions to your Fragment

年华零落成诗 2025-01-28 05:00:42

您需要创建一个扩展片段性并在那里开始片段的类:

public class MyFragmentActivity extends YourActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (savedInstanceState == null){
                getSupportFragmentManager().beginTransaction()
                        .add(android.R.id.content, new MyFragment ()).commit();}
        }
    }

然后片段构造函数:

public MyFragment() {
}

然后从您的调用活动中,以正常方式开始片段活动

   Intent i = new Intent(YourActivity.this, MyFragment.class);
    startActivity(i);

You need to create a class that extends FragmentActivity and start your fragment there:

public class MyFragmentActivity extends YourActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (savedInstanceState == null){
                getSupportFragmentManager().beginTransaction()
                        .add(android.R.id.content, new MyFragment ()).commit();}
        }
    }

then fragment constructor:

public MyFragment() {
}

then from your calling activity, start your fragment activity in the normal way

   Intent i = new Intent(YourActivity.this, MyFragment.class);
    startActivity(i);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文