Xamarin.Android OnActivityResult not being called inside a Fragment

≡放荡痞女 提交于 2019-12-01 08:35:24

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
}

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 
    }
}

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

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