Xamarin.Android OnActivityResult not called inside a fragment

It seems that OnActivityResult is not getting called after taking a picture taken from the camera.

Am I calling StartActivityForResult () wrong? or is there something I am missing.

 public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Android.OS.Bundle savedInstanceState)
        {
            var m_View = inflater.Inflate (Resource.Layout.Feed, null);

            btnCamera = m_View.FindViewById<Button> (Resource.Id.btnCamera);
            btnGallery = m_View.FindViewById<Button> (Resource.Id.btnGallery);

            ivPicture = m_View.FindViewById<ImageView> (Resource.Id.imageView1);

            btnCamera.Click += (sender, e) => {
                //launch gallery
                //allow photo editing/saving

                var mediaPicker = new MediaPicker (Activity);
                if (!mediaPicker.IsCameraAvailable){
//                      Console.WriteLine ("No Camera!");
                }else {
                    Intent intent = new Intent(MediaStore.ActionImageCapture);
                    StartActivityForResult(intent, 0);
                }
            };

            btnGallery.Click += (sender, e) => {
                //launch gallery
                //allow photo editing/saving
                var imageIntent = new Intent ();
                imageIntent.SetType ("image/*");
                imageIntent.SetAction (Intent.ActionGetContent);
                StartActivityForResult (
                    Intent.CreateChooser (imageIntent, "Select photo"), 0);

            };

            return m_View;
        }

        protected virtual void OnActivityResult (int requestCode, Result resultCode, Intent data)
        {
            System.Console.WriteLine ("WOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
            Uri contentUri = data.Data;
            ivPicture.SetImageURI (data.Data);

        }

      

+3


source to share


3 answers


Hmm .. I created a sample and it worked great for me. The only difference I can see is that it looks like your override is wrong. It should be public override void OnActivityResult ()



public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    var rootView = inflater.Inflate(Resource.Layout.MainFragment, container, false);
    var button = rootView.FindViewById<Button>(Resource.Id.button_camera);
    button.Click += (sender, args) =>
    {
        var intent = new Intent(MediaStore.ActionImageCapture);
        StartActivityForResult(intent, 0);
    };
    return rootView;
}

public override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    Log.Debug("TestFragment", "Got result");

    // do what you want with the result here
}

      

+2


source


Check the request code in the same way as for startActivityForResult (intent, 0) here 0 is the request code. For example if you put 999 here insted from 0 .. then in onActivity result method check for requestcode == 999. this is used to check if the intent is the same as the request and nothing else.



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == 0 && resultCode == RESULT_OK) { // ADD THIS
    // Get Data and do something with it 
    }
}

      

0


source


Try changing the datatype resultCode

to int

instead Result

and making an overridden function public

.

eg.

public override void OnActivityResult(int requestCode, int resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
}

      

0


source







All Articles