Search code examples
androidandroid-fragmentsandroid-permissionsandroid-dialogfragment

Android runtime permission call same Fragment after accept or denied permission


Helo guys,

I have activity with component "BottomNavigationView" and in this component I have four fragments. When I click for example on second fragment and click on button "Give permission" here is pop-up for runtime permission for write and read external storage.

When I accept or denied runtime permission application is automatically make callback to MainActivity and show first fragment by default (Because first fragment is default fragment in Main Activitiy).

Is it possible after accept or denied stay on second fragment? Or I need for example save in sharedpreference position of my fragment and invite fragment on this position.

I tried with lib https://github.com/googlesamples/easypermissions. After un success I tried with custom implementation for permission.

Here is my code.

private static final int REQUEST_CODE = 121;
private void requiredUserPermissionForDownloadPDF(){
    if(!permissionAlreadyGranted()){
        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
    }
}


private boolean permissionAlreadyGranted() {
    String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    for (String permission : perms) {
        if (ContextCompat.checkSelfPermission(getContext(), permission) != PackageManager.PERMISSION_GRANTED) {
            return false;
        }
    }
    return true;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == REQUEST_CODE
            && grantResults[0] == PackageManager.PERMISSION_GRANTED
            && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
        System.out.println("Granted");
    }
}

Is it possible after accept or denied stay on second fragment? Or I need for example save in sharedpreference position of my fragment and invite fragment on this position.


Solution

  • I had the same problem, it happens because activity is recreating. You have to save selected item id from BottomNavigationView in onSaveInstanceState method, like this:

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt(KEY_BOT_NAV_VIEW_SELECTED_ITEM_ID, mBottomNavigationView.getSelectedItemId());
        super.onSaveInstanceState(outState);
    }
    

    Where KEY_BOT_NAV_VIEW_SELECTED_ITEM_ID is string key for saving and retrieving id. Then, after this, when your activity is created again and protected void onCreate(Bundle savedInstanceState) is called, bundle argument is not null and you can retrieve saved id:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBottomNavigationView.setOnNavigationItemSelectedListener(this);
        if (savedInstanceState != null) {
            restoreInstanceState(savedInstanceState);
        } else {
            //show first fragment fragment 
        }
    }
    

    You can learn more about saving and restoring state from here