Vojtěch
Vojtěch

Reputation: 12426

onRequestPermissionResult is never called from Activity

I am trying to implement SplashScreenActivity, which will request all necessary permissions and then redirect to the MainActivity:

public class SplashScreenActivity extends Activity {

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        setContentView(R.layout.splash_screen);

        try {
            PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0);
            ((TextView) findViewById(R.id.versionView)).setText(info.versionName);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }


        if (ActivityCompat.checkSelfPermission(this, CAMERA) != PERMISSION_GRANTED
                || ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(this, new String[]{READ_PHONE_STATE, CAMERA}, 200);
        } else {
            onPermissionsReady();
        }
    }



    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 200) {
            onPermissionsReady();
        }
    }

    private void onPermissionsReady() {
        new Fork() {
            @Override public void run() {
                ApplicationContext.getInstance(SplashScreenActivity.this);
                startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));
            }
        };
    }
}

I have two issues with it:

  1. The splash screen design does not show before the Permission request dialog and the screen stays from the android background with application icons.
  2. When you agree with the permissions, the onRequestPermissionsResult is NEVER called and the application ends.

EDIT: I created a sample application here: https://github.com/knyttl/TestApp – it demonstrates both two issues.

EDIT2: This is what happens when i agree/disagree with the permissions requests - the application just ends: https://www.youtube.com/watch?v=lhvhXcEJxLw&feature=youtu.be

Upvotes: 2

Views: 1917

Answers (2)

Vojtěch
Vojtěch

Reputation: 12426

I found out the problem: the activity has noHistory=true, which leads to killing the application as described here:

Requesting Android M permissions from Activity with noHistory="true" and/or showOnLockScreen="true"

Upvotes: 0

R. Zagórski
R. Zagórski

Reputation: 20278

  1. You should extend from AppCompatActivity.
  2. Try to change the line:

    if (ActivityCompat.checkSelfPermission(this, CAMERA) != PERMISSION_GRANTED

to

`if (ActivityCompat.checkSelfPermission(this, Manifest.Permission.CAMERA)  != PERMISSION_GRANTED`
  1. Move the code for checking permission to onResume. Or leave it in onCreate but delay it, for example with Handler

Upvotes: 1

Related Questions