ActivityResultContracts.RequestMultiplePermissions 치트 시트

최근에는 권한 관련 처리가 상당히 간결하게 쓸 수있게되어 기쁩니다.
  • ActivityResultContracts.RequestMultiplePermissions  | Android Developers
  • Getting a result from an activity  |  Android Developers

  • 자바


    private final ActivityResultLauncher<String[]> requestPermissionsLauncher =
            registerForActivityResult(
                    new ActivityResultContracts.RequestMultiplePermissions(),
                    (Map<String, Boolean> grantStates) -> {
                        for (Map.Entry<String, Boolean> grantState : grantStates.entrySet()) {
                            Timber.d(grantState.getKey() + " - " + grantState.getValue());
                        }
                    });
    
    private void requestPermissions() {
        requestPermissionLauncher.launch(new String[]{
                Manifest.permission.Foo,
                Manifest.permission.Bar
        });
    } 
    

    Kotlin


    private val requestPermissionsLauncher =
            registerForActivityResult(RequestMultiplePermissions()) { grantStates: Map<String, Boolean> ->
                for ((permission, granted) in grantStates) {
                    Timber.d("$permission - $granted")
                }
            }
    
    private fun requestPermissions() {
        requestPermissionsLauncher.launch(arrayOf(
                Manifest.permission.Foo,
                Manifest.permission.Bar
        ))
    }
    

    좋은 웹페이지 즐겨찾기