Xamarin.Android OnActivityResult not being called inside a Fragment

前端 未结 4 406
梦毁少年i
梦毁少年i 2021-01-14 04:46

It appears as if OnActivityResult does not get called after accepting the picture taken from the camera.

Am I calling StartActivityForResult() wrong?, or is

相关标签:
4条回答
  • 2021-01-14 05:15

    Hmm.. I created a sample and it worked just fine for me. The only difference I see is that it looks like your override is not correct. 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
    }
    
    0 讨论(0)
  • 2021-01-14 05:19

    For the people that use Android.Support.V4.App Fragment. You can override the OnActivityResult method in your Fragment but it will never be called if you use this in combination with a FragmentActivity! It will be the OnActivityResult of the FragmentActivity that will be called. This can be implemented like following:

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
    }
    
    0 讨论(0)
  • 2021-01-14 05:20

    Try changing the data type of resultCode to int, instead of Result and making the overridden function public.

    e.g.

    public override void OnActivityResult(int requestCode, int resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
    }
    
    0 讨论(0)
  • 2021-01-14 05:22

    Check for request code is same as provided for startActivityForResult(intent, 0) here 0 is request code.. eg if you put 999 here insted of 0 ..then in onActivity result method check for requestcode == 999. it is used to check if the intent is the same as asked for and nothing other than that..

    @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 讨论(0)
提交回复
热议问题