简体   繁体   中英

onActivityResult() not executing in fragment when i call startResolutionForResult

Problem when i am calling to enable gps programatically using GoogleApiClient into fragment... My code is..

 final Status status = result.getStatus();
                final LocationSettingsStates state = result.getLocationSettingsStates();
                switch (status.getStatusCode())
                {
                    case LocationSettingsStatusCodes.SUCCESS:
                        // All location settings are satisfied. The client can initialize location
                        // requests here.
                        getCurrentLocation();
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        // Location settings are not satisfied. But could be fixed by showing the user
                        // a dialog.
                        try {
                            // Show the dialog by calling startResolutionForResult(),
                            // and check the result in onActivityResult().
                            status.startResolutionForResult(getActivity(), REQUEST_ID_GPS_PERMISSIONS);
                        } catch (IntentSender.SendIntentException e) {
                            // Ignore the error.
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        // Location settings are not satisfied. However, we have no way to fix the
                        // settings so we won't show the dialog.
                        break;
                }

and my onActivityResult is

 final Status status = result.getStatus();
                final LocationSettingsStates state = result.getLocationSettingsStates();
                switch (status.getStatusCode())
                {
                    case LocationSettingsStatusCodes.SUCCESS:
                        // All location settings are satisfied. The client can initialize location
                        // requests here.
                        getCurrentLocation();
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        // Location settings are not satisfied. But could be fixed by showing the user
                        // a dialog.
                        try {
                            // Show the dialog by calling startResolutionForResult(),
                            // and check the result in onActivityResult().
                            status.startResolutionForResult(getActivity(), REQUEST_ID_GPS_PERMISSIONS);
                        } catch (IntentSender.SendIntentException e) {
                            // Ignore the error.
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        // Location settings are not satisfied. However, we have no way to fix the
                        // settings so we won't show the dialog.
                        break;
                }

but my onActicvityResult() not executing in fragment. Where is the problem??? Help me.....Thanks in advance.

Use this Line for Fragment to get result in onActivityResult

startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_ID_GPS_PERMISSIONS, null, 0, 0, 0, null);

insted of

 status.startResolutionForResult(getActivity(), REQUEST_ID_GPS_PERMISSIONS);

As fragments are placed inside activities and their life cycle tightly coupled to the life cycle of the containing activity.

1) In Activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Fragment frg = getSupportFragmentManager().findFragmentById(R.id.fragment_container_main);
    if (frg != null) {
        frg.onActivityResult(requestCode, resultCode, data);
    }
}

Now container activity of fragment will provide intent data, request code and result to the fragment so to get data and result in fragment you have to override onActivityResult(int requestCode, int resultCode, Intent data) in fragment as well

2) In Fragment

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

}

there you will get callback from your parent activity.Do whatever you want to send or to get callback.

When you need to resolve the Status or the ResolvableApiException , I suggest you to leverage the activity.registerForActivityResult API in place of startResolutionForResult :

val launcher = activity.registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            // User accepted
        } else {
            // User didn't accepted
        }
    }

val intentSenderRequest = IntentSenderRequest.Builder(exception.resolution).build()
launcher.launch(intentSenderRequest)

I just finished writing some of this same code today. You're on the right track with needing the onActivityForResult() method implemented, but unfortunately startResolutionForResult() doesn't call back to the fragment; it calls back to the activity that contains the fragment.

You must implement onActivityResult() in the calling activity (or wherever your fragments are being managed), and then forward that result to your fragment.

I did something like this in my activities onActivityResult (FragmentBase is just the base class I'm using for all my other fragments, make sure you tag your fragments when you add them):

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        switch (requestCode){
            case LocationHelper.LOCATION_ENABLER_ID:
                FragmentBase mapFrag = (FragmentBase) fragmentManager.findFragmentByTag(FragmentBase.MAP_FRAGMENT);
                ((FragmentMap)mapFrag).returnFromSettings();
                break;
            default:
                super.onActivityResult(requestCode, resultCode, data);
        }
    }

Solution for 2019 [Kotlin] [AndroidX]

1. In Your Fragment:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    Log.d(context!!, "onActivityResult: [From Fragment]: " + requestCode + ", " + resultCode)
}

2. In Your Activity:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    Log.d(this, "onActivityResult: [From Activity]:  " + requestCode + ", " + resultCode)
    val navHostFragment = supportFragmentManager.fragments.first() as? NavHostFragment
    if(navHostFragment != null) {
        val childFragments = navHostFragment.childFragmentManager.fragments
        childFragments.forEach { fragment ->
            fragment.onActivityResult(requestCode, resultCode, data)
        }
    }
}

This will work if you're using Android Navigation Component.

Follow the below steps :

1 . InActivity :

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        YourFragment fragment = (YourFragment ) getSupportFragmentManager().findFragmentByTag("TAG_NAME");
        if (fragment != null) {
            fragment .onActivityResult(requestCode, resultCode, data);
        }

    }
  1. In Fragment

     @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // Now in fragment will triggger Here you can do work }

If you are running this code in Fragment than don't use startResolutionForResult(). Instead use startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_CODE_LOCATION_SETTING, null, 0, 0, 0, null);

and overrider onaActivityResult() in your fragment. Result will be delivered to this method only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM