Search code examples
androidandroid-intentandroid-activityxamarin.androidonactivityresult

OnActivityResult Android is not triggered


I've a problem with Xamarin.Android, when I scanned a QRCode who contain some products infos and launch an activity (StartActivityForResult) for borrow and finish the other activity, it doesn't trigger OnActivityResult.

When I start the activity:

    scannerButton.Click += async delegate
{
    MobileBarcodeScanner.Initialize(Application);

    var scanner = new MobileBarcodeScanner();

    var qrValue = await scanner.Scan();

    if (qrValue != null)
    {
        string[] infosProduct = qrValue.Text.Split(',');

        ProgressDialog progress = new ProgressDialog(this);
        progress.SetMessage("En cour...");
        progress.SetTitle("Vérification du statut de l'objet");
        progress.SetProgressStyle(ProgressDialogStyle.Spinner);
        progress.SetCancelable(false);
        progress.Show();

        bool? borrowed = DbManager.getInstance().isProductBorrowed(Convert.ToInt32(infosProduct[0]));

        progress.Dismiss();

        if (borrowed == true) 
        {                     
            Toast.MakeText(this, "todo : rendre produit", ToastLength.Long).Show();
        }
        else if (borrowed == false)
        {
            Intent intent = new Intent(this, typeof(BorrowActivity));
            intent.PutExtra("user", settings.GetString("user", ""));
            intent.PutExtra("infosProduct", infosProduct);
            StartActivityForResult(intent,0);
        }
    }
};

The onResult

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        if (resultCode == Result.Ok)
        {
            // TODO Refresh
            Toast.MakeText(this, "test", ToastLength.Long).Show();
        }
    }

When I send result:

borrowButton.Click += delegate {

Intent intent = new Intent(this, typeof(MenuActivity));
                        SetResult(Result.Ok, intent);
                        Finish();
                    }

Solution

  • Problem solved, the problem was that my Activiy where I have "OnActivityResult" hasn't the param "MainLauncher = true" or it just solves with recreate the project. I used @Justinas answer too but it works only when I recreate project and use the param. So I don't really know for which reason it works, but it works c: