我有两个活动,一个是InventoryActivity
,第二个是StoneDetailActivity
。在我的InventoryActivity有RecycleView
在RecycleView按钮点击我开始使用StartActivityForResult
下面的代码的StoneDetailActivity
。
Intent stonedetailIntent = new Intent(context, typeof(StoneDetailActivity));
stonedetailIntent.PutExtra("SearchitemObject", stoneJson);
stonedetailIntent.PutExtra("position", position);
context.StartActivityForResult(stonedetailIntent, 1000);
context.OverridePendingTransition(Resource.Animation.Slide_in_right, Resource.Animation.Fade_back);
在StoneDetailActivity
按钮中单击我使用此代码来完成当前活动并转到OnBackPressed()
。
public override void OnBackPressed()
{
Intent intent = new Intent();
intent.PutExtra("BoolCheck", postflag);
intent.PutExtra("Position", position);
SetResult(Result.Ok, intent);
Finish();
}
和在InventoryActivity
中我设置了这个代码。
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode == Result.Ok)
{
bool checkflag = data.GetBooleanExtra("BoolCheck", false);
int position = data.GetIntExtra("Position", -1);
if (checkflag && position > -1)
{
searchItems.RemoveAt(position);
inventAdapter.NotifyDataSetChanged();
txt_totalStone.Text = searchItems.Count.ToString();
txt_totalCarat.Text = searchItems.Sum(c => c.Weight.Value).ToString();
txt_totalAmount.Text = searchItems.Sum(c => c.Rate.Value).ToString();
mainActivityBool = true;
badgeCounttextView.Text = BizApplication.BADGE_COUNT.ToString();
}
}
}
按钮点击代码:
add_to_cart_button.Click += async (sender, e) =>
{
ProgressDialog pdialog = new ProgressDialog(this);
pdialog.SetMessage("Please Wait...");
pdialog.Show();
cartItem = new CartItem();
cartItem.StoneId = searchItem.PacketId;
cartItem.UserId = BizApplication.getCredential().Id;
cartItem.Amount = searchItem.Rate.Value;
cartItem.Discount = searchItem.Discount;
postflag = await InventoryService.AddToCart(cartItem);
if (postflag)
{
OnBackPressed();
BizApplication.BADGE_COUNT += 1;
}
pdialog.Dismiss();
};
这段代码第一次工作很好。但是,如果我做同样的过程,StoneDetailActivity
设置打开,即使我点击完成。
更新:
当我完全调试我的代码,我发现,当我点击第二次OnBackPressed()。并完成它,我的调试再次启动OnCreate
活动,这就是它发生的原因。但是我不会重新开始,那么为什么会发生呢?
根据这篇文章,问题是在ListView
或RecycleView
内部,如果我们执行一些任务,如OnclickListener
,那么我们必须检查OnclickListener
,否则它将触发多个事件。
if (!button.HasOnClickListeners)
{
button.Click += this.clickHandler;
}
那么代码运行正常。
详细信息请访问:https://forums.xamarin.com/discussion/9244/single-click-on-button-invoking-multiple-clicks
尝试添加标志Intent
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);