0

I struct at first page, I am not able to open Dialog soon after permission granted(Write). First time I asked Permission and I granted Permission,in onRequestPermissionsResult() I opened Dialog, here App is closed with Unfortunately APP has crashed issue. When reopen App the issue not here, It is running normally.I got issue in Monitor
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
.I Searched lot but different answers I've seen.Please help me ,I am not getting why app is crashed first time. Here is my code:

 llAddPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!tvAddPhotoOrNext.getText().toString().equals("Next")) { // When Image not gone selected
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        //First checking if the app is already having the permission
                        if (isReadStorageAllowed()) {
                            //If permission is already having then showing the toast
                            Toast.makeText(AddUserProfilePic.this, "You already have the permission", Toast.LENGTH_LONG).show();
                            //Existing the method with return
                            showFourButtonsBSDialog();
                        }else {
                            //If the app has not the permission then ask for the permission
                            checkPermissions();
                        }
                    } else
                        showFourButtonsBSDialog();
                } else {        // When image gone selected.
                    mSetProfilePicByAPI();
                }

            }
        });


@SuppressLint("InlinedApi")
private void checkPermissions() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PICKIMAGE);
    } else {

    }
}


 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    switch (requestCode) {
        case REQUEST_PICKIMAGE:
            //If permission is granted
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //Displaying a toast
                Toast.makeText(this, "Permission granted now you can read the storage", Toast.LENGTH_LONG).show();
                showFourButtonsBSDialog();
            } else {
                //Displaying another toast if permission is not granted
                Toast.makeText(this, "Oops you just denied the permission", Toast.LENGTH_LONG).show();
            }
    }
}


   /* BottomSheetDialog */
private void showFourButtonsBSDialog() {

    MoreOptionsDialogFragment mTBFragment  = new MoreOptionsDialogFragment();   // ThreeButtonsFragment
    Bundle mTextNamesArgs = new Bundle(); //Send buttons names
    mTextNamesArgs.putStringArray("buttons_names",new String[]{"Take Photo","Choose from library",
    "Photo from Facebook","Photo from Instagram"});
    mTBFragment.setArguments(mTextNamesArgs);
    mTBFragment.show(getSupportFragmentManager(),mTBFragment.getTag());
}


 private boolean isReadStorageAllowed() {
    //Getting the permission status
    int result = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
    //If permission is granted returning true
    if (result == PackageManager.PERMISSION_GRANTED)
        return true;
    //If permission is not granted returning false
    return false;
}
Lokesh
  • 316
  • 3
  • 14

1 Answers1

0

What are your compile SDK and target SDK versions? Make sure them to be 23 or higher. https://developer.android.com/guide/topics/permissions/requesting.html

Fio
  • 3,088
  • 2
  • 13
  • 23