android prompt user to enable location services intent error

628 Views Asked by At

I have an app that needs to use location services for one of its features. So when the user clicks on a button, I would like my app to check to see whether the device has location services enabled, and if it doesn't prompt the user to enable it via an AlertDialog. When the user clicks the 'Enable Location Services' button, I want the app to take the user to the settings page where the user can enable it.

My problem is that when the user clicks the 'Enable Location Services' button, instead of opening up the settings page, the app goes straight into the main activity.

Note: This is all happening inside of a fragment whose parent activity is my second Activity. So here is the layout of my app: MainActivity (login page) => Second Activity (parent activity) => fragment (where this check must occur).

Here is my code inside of the fragment's onCreateView method:

            currentLoc.setOnClickListener(new View.OnClickListener() {
                DataActivity dataActivity = (DataActivity) getActivity();
                Context context = getActivity().getApplicationContext();
                @Override
                public void onClick(View view) {
                    if (!dataActivity.isLocationServiceEnabled()){
                        final AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
                        alertDialog.setTitle("Alert");
                        alertDialog.setMessage("To use this feature, you must first enable location services");

                        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Enable Location Services", new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int id) {
                                Intent gpsOptionsIntent = new Intent(
                                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                startActivity(gpsOptionsIntent);
                            }});

                        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int id) {
                                dialog.dismiss();
                            }});
                        alertDialog.show();
                    }
                    else {
                        AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
                        alertDialog.setTitle("Alert");
                        alertDialog.setMessage("GPS Enabled...");
                        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                });
                        alertDialog.show();
                    }
                }
            });

Note: currentLoc is an imageView that is created inside the fragment_layout file

More information:

  • compileSdkVersion 26
  • buildToolsVersion "25.0.3"
  • minSdkVersion 14
  • targetSdkVersion 26
  • compile 'com.android.support:appcompat-v7:26+'
  • compile 'com.android.support:support-v4:26+'

I have tried countless variations that all use intents and for this project they all take me back to the login page (MainActivity). When I create a new project and use the same code, it works...

0

There are 0 best solutions below