Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start resolution for result in a fragment

Tags:

In my app if the user doesn't have the location turned on I am prompting with a dialog and then trying to return that result (probably incorrectly) by overriding on activity result.

This is inside a fragment so not sure how that changes things:

This is how I am calling it the dialog with startResolutionForResult:

public void checkDeviceLocationIsOn()         {             System.out.println("Test running setting request" );             LocationRequest locationRequest = LocationRequest.create();             locationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);             LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()                     .addLocationRequest(locationRequest);             builder.setAlwaysShow(true); //this is the key ingredient              PendingResult<LocationSettingsResult> result =                     LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());             result.setResultCallback(new ResultCallback<LocationSettingsResult>() {                 @Override                 public void onResult(LocationSettingsResult result) {                     final Status status = result.getStatus();                     final LocationSettingsStates state = result.getLocationSettingsStates();                     switch (status.getStatusCode()) {                         case LocationSettingsStatusCodes.SUCCESS:                             // All location settings are satisfied.                             System.out.println("Test setting all fine starting location request" );                             getLocation();                             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(), LOCATION_SETTINGS_REQUEST_CODE);                                 System.out.println("Test setting not met starting dialog to prompt user" );                             } 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 then I try to get the result like this below it (This never gets called):

   @Override     public void onActivityResult(int requestCode, int resultCode, Intent data) {         switch (requestCode) {         // Check for the integer request code originally supplied to startResolutionForResult().             case LOCATION_SETTINGS_REQUEST_CODE:                 switch (resultCode) {                     case Activity.RESULT_OK:                         System.out.println("test user has turned the gps back on");                         getLocation();                         break;                     case Activity.RESULT_CANCELED:                          System.out.println("test user has denied the gps to be turned on");                         Toast.makeText(getActivity(), "Location is required to order stations", Toast.LENGTH_SHORT).show();                         break;                 }                 break;         }     } 

Thanks in advance for your help

like image 419
Nicholas Muir Avatar asked Oct 18 '16 14:10

Nicholas Muir


People also ask

What is start activity for result?

The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.

Can I use onActivityResult in fragment?

Since Activity gets the result of onActivityResult() , you will need to override the activity's onActivityResult() and call super. onActivityResult() to propagate to the respective fragment for unhandled results codes or for all.

How do I get data from startActivityForResult?

First you use startActivityForResult() with parameters in the first Activity and if you want to send data from the second Activity to first Activity then pass the value using Intent with the setResult() method and get that data inside the onActivityResult() method in the first Activity .

What is onActivityResult?

onActivityResult is the callback you have on the first activity to grab the contacts you choose. Follow this answer to receive notifications.


1 Answers

Call the fragment method

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

instead of

status.startResolutionForResult(...)

like image 135
Badhrinath Canessane Avatar answered Sep 17 '22 00:09

Badhrinath Canessane