Android application restarts while selecting the location permission

Viewed 25

I am trying to build an application that ask for the location permission. I am trying to resolve an issue that application restarts while selecting the location permission. So what happens is when I select the option "while using the app.." the app restarts and goes to * Main.class*. What am I doing wrong here. Any help would be appreciated. This is my code for intent to go to permission class.

 Intent intent = new Intent( getApplicationContext(), Permissions.class );
                         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                         startActivity( intent );
                         overridePendingTransition( R.anim.enter_from_right, R.anim.exit_to_left );
                         finish();

This is my Permission class where I have handled the permission interactions.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_permissions);
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    ac = this;
    btnAskPerm = findViewById(R.id.btnAskPerm);
    btnAskPerm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            System.out.println("share location button clickeed");
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                //Permission check and ask for required permission
                Dexter.withActivity(ac)
                        .withPermissions(
                                Manifest.permission.ACCESS_FINE_LOCATION,
                                Manifest.permission.ACCESS_COARSE_LOCATION
                        ).withListener(new MultiplePermissionsListener() {
                            @Override
                            public void onPermissionsChecked(MultiplePermissionsReport report) {
                                // check if all permissions are granted
                                if (report.areAllPermissionsGranted()) {
                                    gotoLanding();

                                }

                                // check for permanent denial of any permission
                                if (report.isAnyPermissionPermanentlyDenied()) {
                                    // permission is denied permenantly, navigate user to app settings
                                    AlertDialog.Builder builder1 = new AlertDialog.Builder(ac);
                                    builder1.setMessage(R.string.select_locations);
                                    builder1.setCancelable(true);
                                    builder1.setPositiveButton(
                                            R.string.ok,
                                            new DialogInterface.OnClickListener() {
                                                public void onClick(DialogInterface dialog, int id) {
                                                    dialog.cancel();
                                                }
                                            });
                                    AlertDialog alert11 = builder1.create();
                                    alert11.show();
                                }

                            }

                            @Override
                            public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
                                token.continuePermissionRequest();
                            }

                        }).check();

            } else {

                gotoLanding();
            }
        }
    });

}
 public void gotoLanding() {
            Intent intent = new Intent(getApplicationContext(), Main.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
            overridePendingTransition(R.anim.enter_from_right, R.anim.exit_to_left);
            finish();

}
1 Answers

I would assume it's because of the flags in your Intent. Try to remove them where it's not necessary to actually clear the old activity and start a new one:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Related