This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| applyButton.setOnClickListener { | |
| setFragmentResult( | |
| "request_key", | |
| bundleOf("extra_key" to getSelectedSort()) | |
| ) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setFragmentResultListener("request_key") { key, bundle -> | |
| val selectedSort = bundle.getParcelable<Sort>("extra_key") | |
| // applying the obtained sorting | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class NewActivity : AppCompatActivity(R.layout.a_main) { | |
| val permission = registerForActivityResult(RequestPermission()) { granted -> | |
| when { | |
| granted -> { | |
| camera.launch() // access to the camera is allowed, open the camera | |
| } | |
| !shouldShowRequestPermissionRationale(Manifest.permission.CAMERA) -> { | |
| // access to the camera is denied, the user has checked the Don't ask again. | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class PermissionsActivity : AppCompatActivity(R.layout.a_main) { | |
| val singlePermission = registerForActivityResult(RequestPermission()) { granted -> | |
| when { | |
| granted -> { | |
| // access to the camera is allowed, open the camera | |
| } | |
| !shouldShowRequestPermissionRationale(Manifest.permission.CAMERA) -> { | |
| // access to the camera is denied, the user has checked the Don't ask again. | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| vButton.setOnClickListener { | |
| activityLauncher.launch("What is the answer?") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val activityLauncher = registerForActivityResult(MySecondActivityContract()) { result -> | |
| // using result | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MySecondActivityContract : ActivityResultContract<String, Int?>() { | |
| override fun createIntent(context: Context, input: String?): Intent { | |
| return Intent(context, SecondActivity::class.java) | |
| .putExtra("my_input_key", input) | |
| } | |
| override fun parseResult(resultCode: Int, intent: Intent?): Int? = when { | |
| resultCode != Activity.RESULT_OK -> null | |
| else -> intent?.getIntExtra("my_result_key", 42) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| implementation 'androidx.activity:activity-ktx:1.3.0-alpha04' | |
| implementation 'androidx.fragment:fragment-ktx:1.3.1' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class OldActivity : AppCompatActivity(R.layout.a_main) { | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| vButtonCamera.setOnClickListener { | |
| when { | |
| checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED -> { | |
| // access to the camera is allowed, open the camera | |
| startActivityForResult( |