Search code examples
androidandroid-activityxamarin.androidactivity-finishonbackpressed

Finish Activity not close in Android (Xamarin)


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.


Solution

  • 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