Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:
Subscribe to my newsletter: wajahatkarim.com/subscribe

Wajahat Karim wajahatkarim3

:octocat:
Subscribe to my newsletter: wajahatkarim.com/subscribe
View GitHub Profile
View Permissions.kt
// Requesting Location Permission
bi.btnRequestPermission.setOnClickListener {
askLocationPermission(android.Manifest.permission.ACCESS_FINE_LOCATION)
}
// Single Permission Contract
private val askLocationPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { result ->
if(result){
Log.e("TAG", "Location permnission granted")
}else{
View PickImages.kt
// Pick Images Contract - Normally this is for all kind of files.
private val pickImages = registerForActivityResult(ActivityResultContracts.GetContent()) {uri ->
uri?.let {uri ->
imageView.setImageURI(uri)
}
}
// Calling GetContent contract
pickImageButton.setOnClickListener {
pickImages("image/*") // We want images, so we set the mimeType as "image/*"
View TakePicture.kt
// TakePicture Contract Registration
private val takePicture = registerForActivityResult(ActivityResultContracts.TakePicture()) {bitmap ->
bitmap?.let {
imageView.setImageBitmap(bitmap)
}
}
// Calling the takePicture contract
captureButton.setOnClickListener {
var imageUri: Uri? = null
View CallingContract.kt
btnPick.setOnClickListener {
simpleContractRegistration(input = 3) // Remember, our SimpleContract had input of Int. So, 3 is dummy value here.
}
View MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
// Other Methods
private val simpleContractRegistration = registerForActivityResult(SimpleContract()) {resultStr ->
View SimpleContract.kt
class SimpleContract : ActivityResultContract<Integer, String?>() {
override fun createIntent(context: Context, input: Integer?): Intent {
var intent = Intent(context, MyChildActivity::class.java)
intent.putExtra("myInputKey", input)
return intent
}
override fun parseResult(resultCode: Int, intent: Intent?): String? = when
{
View Dependencies.gradle
implementation 'androidx.activity:activity-ktx:1.2.0-alpha04'
implementation 'androidx.fragment:fragment-ktx:1.3.0-alpha04'
@wajahatkarim3
wajahatkarim3 / MainActivity.kt
Created May 1, 2020
The onActivityResult() method example
View MainActivity.kt
class MainActivity : AppCompatActivity()
{
// Other methods of your Activity
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Capture Image with Camera
if (requestCode == CAMERA_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) {
if (data != null) {
View MainActivity.kt
private var backPressedOnce = false
override fun onBackPressed()
{
var navController = findNavController(R.id.fragNavHost)
// Check if the current destination is actually the start sestination (Home screen)
if (navController.graph.startDestination == navController.currentDestination?.id)
{
// Check if back is already pressed. If yes, then exit the app.
View NotificationDetailsFragment.kt
override fun onAttach(context: Context) {
super.onAttach(context)
(activity as MainActivity).hideBottomNavigation()
}
override fun onDetach() {
(activity as MainActivity).showBottomNavigation()
super.onDetach()
}
You can’t perform that action at this time.