如何在关闭时将产品保存在购物篮中,更改活动

本文关键字:购物篮 存在 活动 保存 | 更新日期: 2023-09-27 18:20:14

我的应用程序中有产品篮。在c#(Xamarin)上为android编写

但当我关闭/更改活动时,它会删除。

将其他活动中的产品添加到购物篮中的代码。

add.Click += delegate {
    var intent = new Intent (this, typeof(CartActivity));
    intent.PutExtra ("title", (string)(firstitem ["post_title"]));
    intent.PutExtra ("price", (string)(firstitem ["price"] + " грн"));
    intent.PutExtra ("weight", (string)(firstitem ["weight"] + "г"));
    StartActivity (intent);
};

收货吊篮活动代码

productname.Text = Intent.GetStringExtra("title");
price.Text = Intent.GetStringExtra("price");
weight.Text = Intent.GetStringExtra("weight");

如何将产品保存在购物篮中?

如何在关闭时将产品保存在购物篮中,更改活动

但当我关闭/更改活动时,它会删除。

因为在不保存任何位置的情况下将数据发送到下一个"活动",所以如果任何其他"活动"正在启动或应用程序重新打开,则使用getIntent方法,数据篮活动中的数据不可用。

简单的方法是使用SharedPreferences来存储和检索数据:

示例:如何保存用户设置