Android Fragments 按下后碎片会被破坏,我希望按下后碎片不会被破坏,以便保存碎片的状态

2exbekwf  于 2022-11-24  发布在  Android
关注(0)|答案(1)|浏览(136)

我创建了一个Activity并向其中添加了3个片段。一个是HomeFragment,第二个是ProductDetailFragment,第三个是CartFragment。我从ProductDetailFragment添加了多个数量的产品,我希望保存数量详细信息,即使我返回HomeFragment并再次返回ProductDetailFragment。但是,只要我按下返回键转到HomeFragment,ProductDetailFragment被销毁,如果再次出现ProductDetailFragment,则数量值全部被清除。
我已经尝试过onSaveInstanceState来存储状态,即使片段被破坏,但在这种情况下不起作用。让我给你看我的代码:
产品详细信息片段

lateinit var binding: FragmentProductDetailBinding
    var quantityValue = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Toast.makeText(activity,"onCreateCalled",Toast.LENGTH_SHORT).show()
        if(savedInstanceState == null) {
            quantityValue = 0
        } else {
            quantityValue = savedInstanceState.getInt("quantity",0)
        }

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
       /// return inflater.inflate(R.layout.fragment_product_detail, container, false)

        binding = FragmentProductDetailBinding.inflate(inflater,container,false)
        binding.productDetails1 = arguments?.getParcelable("product")
        var productDetail = binding.productDetails1

       // var priceAmount = binding.priceAmount.text.toString().toFloat()

        Log.d("PriceAmount", binding.priceAmount.text.toString())

       // Log.d("ProductTitle", binding.productDetails1.title)
                binding.addButton.setOnClickListener {
                // Log.d("ProductDetail",initialValue.toString())
                if (productDetail == null) return@setOnClickListener
                quantityValue = quantityValue + 1
                binding.totalPriceText.text = "Total price"
                binding.quantityText.text = quantityValue.toString()
                binding.priceAmount.text = (productDetail.price * quantityValue).toString()

                // Log.d("ProductDetail",priceAmount.toString())
            }
            binding.removeButton.setOnClickListener {
                if (quantityValue != 0) {
                    if (productDetail == null) return@setOnClickListener
                    quantityValue = quantityValue - 1
                    binding.quantityText.text = quantityValue.toString()
                    //   binding.priceAmount.text = (priceAmount*quantityValue).toString()
                    binding.priceAmount.text = (productDetail.price * quantityValue).toString()

                }
            }

            binding.addCartButton.setOnClickListener{
            val bundle = Bundle()
            bundle.putString("quantity_text", String.format("%d",quantityValue))
            val cartFragment = CartFragment()
                cartFragment.arguments = bundle

            val ft = fragmentManager?.beginTransaction()
            ft?.replace(R.id.flFragment,cartFragment)
            ft?.addToBackStack("")
            ft?.commit()
            // Create the transaction
            // Create the transaction
        }
        Toast.makeText(activity,"onCreateViewCalled",Toast.LENGTH_SHORT).show()

        return binding.root
    }

  override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putInt("quantity", quantityValue)

    }

家庭片段
第一次
enter code here } }

wwodge7n

wwodge7n1#

您可以使用SharedViewModel,因为两个片段共享相同的Activity。您可以添加要保留的数据,并且当您转到详细页视图模型时,该模型仍具有您需要的数据,因为它取决于Activity的生命周期。
官方文件:https://developer.android.com/topic/libraries/architecture/viewmodel#sharing

相关问题