Search code examples
androidandroid-activityresultset

requestCode code is returning same value either change the you check option for gps or not


I am calling the following method:

Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, GPS_LOC);

but on onActivityResultas below method

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != -1) {
           switch (requestCode) {
              case GPS_LOC:
                userData();
               break;
            }
         }  
     }

it's "requestCode" and "resultCode" returning same value either you "on" the gps setting or returning without doing "on". I don't want to call userData(); when user didn't turn "on" gps setting. Please look into matter


Solution

  • The issue is you cannot start the Activity:

    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS
    

    for a result.

    Therefore your requestCode is never returned from the activity.

    Ask yourself why you want to start this for a result? Is it to check if they have turned on the GPS? If it is so, when the activity returns just check it again. If it is for another reason I don't think you grasp what startActivityForResult's use is.

    Related question: getting-back-from-location-to-my-application