Home » Android » android – onActivityResult() not executing in fragment when i call startResolutionForResult

android – onActivityResult() not executing in fragment when i call startResolutionForResult

Posted by: admin June 15, 2020 Leave a comment

Questions:

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.

How to&Answers:

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.

Answer:

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

Answer:

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

Answer:

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
    
    }
    

Answer:

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.