Finish Activity not close in Android (Xamarin)

為{幸葍}努か 提交于 2020-01-03 05:15:11

问题


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);

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();
 }

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();

                }

            }
        }

Button Click code :

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();

                };

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

UpDate :

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.


回答1:


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.

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




回答2:


try by adding flag Intent

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);


来源:https://stackoverflow.com/questions/40233422/finish-activity-not-close-in-android-xamarin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!