本文介绍了完成活动无法在Android(Xamarin)中关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个活动,一个是InventoryActivity,第二个是StoneDetailActivity.在我的InventoryActivity中,在RecycleView按钮中单击RecycleView,然后单击StartActivityForResult下面的代码以启动StoneDetailActivity.

I have Two Activity one is InventoryActivity and Second is StoneDetailActivity. In my InventoryActivity have RecycleView In RecycleView Button Click I start the StoneDetailActivity using StartActivityForResult below code.

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()".

In StoneDetailActivity Button click I use this code to Finish the current Activity and go to OnBackPressed().

public override void OnBackPressed()
 {
    Intent intent = new Intent();
    intent.PutExtra("BoolCheck", postflag);
    intent.PutExtra("Position", position);
    SetResult(Result.Ok, intent);
    Finish();
 }

并且在InventoryActivity中,我已经设置了此代码.

and In InventoryActivity I have set this code.

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设置也会打开.

this code work fine for first Time. But Again if I do the same process, the StoneDetailActivity set open eventhough if I click finish.

更新日期:

当我完全调试我的代码时,我发现当我单击第二次OnBackPressed()时.并完成它,然后再次启动OnCreate活动,这就是发生它的原因.但是我不会再开始了,为什么会发生.

When I full debug my code and i found that when I click on Second time OnBackPressed(). and Finish it my debug again start the OnCreate activity that's why it happening. But I am not starting Again then Why is Happening.

我不明白会发生什么.任何帮助都将受到感激.

What happen I don't understand. Any Help be Appreciated.

推荐答案

根据此帖子,问题是在ListViewRecycleView内部,如果我们执行的某些任务如OnclickListener,则我们检查的是像下面这样,它将触发多个事件.

As per this Post the Problem was that inside ListView or RecycleView if we are perform some task like OnclickListener then we have check is OnclickListener like below way other it will fire multiple event.

 if (!button.HasOnClickListeners)
    {
        button.Click += this.clickHandler;
    }

然后代码可以正常工作.

Then the code is working fine.

有关更多详细信息,请访问: https://forums.xamarin.com/discussion/9244/单击按钮多次调用

For more detail visit this :https://forums.xamarin.com/discussion/9244/single-click-on-button-invoking-multiple-clicks

这篇关于完成活动无法在Android(Xamarin)中关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 22:13