完成活动未关闭在Android (Xamarin)



我有两个活动,一个是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活动,这就是它发生的原因。但是我不会重新开始,那么为什么会发生呢?

我不明白发生了什么事。

根据这篇文章,问题是在ListViewRecycleView内部,如果我们执行一些任务,如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);

相关内容

  • 没有找到相关文章

最新更新