16

I check for the required permissions on my Log In screen. The dialog shows asking for the 4 permissions I need. All good, but when the dialog appears, the background becomes black and my app closes (doesn't crash, just closes). When I'm done with choosing the permissions I open the app again and it continues running from where it stopped. How can I make the app continue running while the permissions dialog is shown? I use a class which checks for permissions and call it in the Log In activity.

The class:

public abstract class RuntimePermissionsActivity extends AppCompatActivity {
private SparseIntArray mErrorString;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    mErrorString = new SparseIntArray();
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    int permissionCheck = PackageManager.PERMISSION_GRANTED;
    for (int permission : grantResults){
        permissionCheck = permissionCheck + permission;
    }
    if ((grantResults.length>0) && permissionCheck == PackageManager.PERMISSION_GRANTED){
        onPermissionsGranted(requestCode);
    }
    else {

                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setData(Uri.parse("package:" + getPackageName()));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                startActivity(intent);
    }
}

public void requestAppPermissions(final String[] requestedPermissions, final int requestCode){
    int stringId = 0;
    mErrorString.put(requestCode, stringId);
    int permissionCheck = PackageManager.PERMISSION_GRANTED;
    boolean shouldShowRequestPermissionRationale = false;
    for (String permission : requestedPermissions){
        permissionCheck = permissionCheck + ContextCompat.checkSelfPermission(this, permission);
        shouldShowRequestPermissionRationale = ActivityCompat.shouldShowRequestPermissionRationale(this, permission);
    }
    if(permissionCheck != PackageManager.PERMISSION_GRANTED){
        if(shouldShowRequestPermissionRationale){
                    ActivityCompat.requestPermissions(RuntimePermissionsActivity.this, requestedPermissions, requestCode);
        }
        else {
            ActivityCompat.requestPermissions(this, requestedPermissions, requestCode);
            Toast.makeText(RuntimePermissionsActivity.this, "Please relaunch the application in order for the changes to take effect!", Toast.LENGTH_LONG).show();
        }
    }
    else {
        onPermissionsGranted(requestCode);
    }

}
public abstract void onPermissionsGranted(int requestCode);
}

The Log In activity call:

LoginActivity.super.requestAppPermissions(new
            String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSIONS);
Floern
  • 33,559
  • 24
  • 104
  • 119
N. Park
  • 387
  • 4
  • 14
  • Cant understand what you are actually doing – Mohammed Atif Aug 09 '16 at 07:53
  • @MohammedAtif Checking if permissions are granted on run time, because after Android M the permissions are not being granted during installation, but after the app is started. – N. Park Aug 09 '16 at 08:00
  • @N.Park check my answer http://stackoverflow.com/a/33163206/3395198 – IntelliJ Amiya Aug 09 '16 at 08:03
  • 1
    @IntelliJAmiya Thank you. I tried your answer, but the screen still went black, this time showing only one dialog for the first permission. – N. Park Aug 09 '16 at 08:33
  • @You should add both permission – IntelliJ Amiya Aug 09 '16 at 08:35
  • I completely understand the runtime permissions and have implemented it multiple times. But i dont understand the use of separate activity to ask permissions. And a very bad use of rationale request. – Mohammed Atif Aug 09 '16 at 08:35
  • 1
    You are not even inflating any view or adding proper actions to this special permission requesting activity. That's why you get a blank screen and app closes. Try implementing the permissions in your loginActivity itself instead of creating additional classes. – Mohammed Atif Aug 09 '16 at 08:38
  • @MohammedAtif what view do you think I should inflate? – N. Park Aug 09 '16 at 09:33
  • You should avoid using separate class for asking permissions. Ask permissions only when needed that too in same activity. – Mohammed Atif Aug 09 '16 at 10:19

5 Answers5

17

I faced same issue,check if activity android:noHistory="true" in your Manifest file.

CHarris
  • 2,693
  • 8
  • 45
  • 71
ankit jain
  • 171
  • 1
  • 4
11

Same problem here.

ankit jain was on the track. Removing android:noHistory="true" fixed the N.Park problem.

Although in my case I needed that value to be true, or at least the behaviour that makes (I had a SplashScreen where I managed the permissions, and after moving on, I wanted that Activity out of my stack).

So:

  1. I deleted line android:noHistory="true"

  2. In the activity I manage the permission without problems

  3. After that, I move into the next activity with:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
DanixDani
  • 186
  • 1
  • 6
6

An example of implementing Permissions in any Activity.

SampleActivity.java

public class SampleActivity extends AppCompatActivity{
    private final int PERMISSION_CODE = 1;
    Button button;
    @override
    onCreate(Bundle savedInstance){
        super.onCreate(savedInstance);
        setContentView(R.layout.your_layout);
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener(){
            @override
            public void onClick(View view){
                requestPermissionAndContinue();
            }
        });
        //remaining code to continue using the app
        //your actual code should also be in this same class
    }

    private void requestPermissionAndContinue(){
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){
            if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)){
                Log.e(TAG, "permission denied, show dialog");
            }else{
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, PERMISSION_CODE);
            }
        }else{
            accessContacts();
        }
    }

    private void accessContacts(){
        //your code once you receive permission
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if(grantResults.length > 0 && permissions[0]==PackageManager.PERMISSION_GRANTED){
            accessContacts();
        }else{
             //redirect to settings page or ask permission again
        }
    }
}
Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57
2

Remove android:noHistory="true" attribute from respective RuntimePermissionsActivity tag from Android manifest file solved that problem.

Dishant Walia
  • 701
  • 5
  • 8
0

Write just a few lines of codes:

 final int requestCode=100;
 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {
     String[] per = {Manifest.permission.READ_CONTACTS};
     requestPermissions(per, requestCode);

     if (ActivityCompat.checkSelfPermission(CollectZone.this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
         // recall permission until is not permitted
     } else { 
         // write execution code here   
     }
 }
Fabian N.
  • 3,807
  • 2
  • 23
  • 46
Pradeep Sheoran
  • 493
  • 6
  • 15