Skip to content

Instantly share code, notes, and snippets.

View Devansh-Maurya's full-sized avatar
Up and up and up...

Devansh Maurya Devansh-Maurya

Up and up and up...
View GitHub Profile
/**
* Returns true if the navigation controller is still pointing at 'this' fragment, or false if it already navigated away.
*/
fun Fragment.mayNavigate(): Boolean {
val navController = findNavController()
val destinationIdInNavController = navController.currentDestination?.id
// add tag_navigation_destination_id to your ids.xml so that it's unique:
val destinationIdOfThisFragment = view?.getTag(R.id.tag_navigation_destination_id) ?: destinationIdInNavController
/**
* Navigates only if this is safely possible; when this Fragment is still the current destination.
*/
fun Fragment.navigateSafe(
@IdRes resId: Int,
args: Bundle? = null,
navOptions: NavOptions? = null,
navigatorExtras: Navigator.Extras? = null
) {
if (mayNavigate()) findNavController().navigate(
@naman14
naman14 / ClickMonitoringView.kt
Created January 18, 2021 17:19
Handle possible child clicks in a parent viewgroup
package com.bharatpe.nativeloader
import android.content.Context
import android.graphics.Rect
import android.util.AttributeSet
import android.util.Log
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
@gmk57
gmk57 / 1 ViewBindingDelegates.kt
Last active June 29, 2025 07:54
Kotlin delegates for Android View Binding with usage examples
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.viewbinding.ViewBinding
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
/**
* Friendly reminder that this might not be the best place to do major changes. Or any changes at all.
* Use the [reason] arg to point your teammates and your future-self to some documentation.
*/
annotation class HereBeDragons(val reason: String)