Created
April 24, 2025 11:13
-
-
Save JEuler/be0bb887d785ba9d49f961ddc625bd54 to your computer and use it in GitHub Desktop.
ViewBindingBaseFragment for ViewBinding usage in Fragments.
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
import android.os.Bundle | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import androidx.fragment.app.Fragment | |
import androidx.viewbinding.ViewBinding | |
/** | |
* Base fragment that handles ViewBinding lifecycle with enhanced safety mechanisms | |
* | |
* This class provides: | |
* 1. Proper binding lifecycle management | |
* 2. Safe access to binding with null checks | |
* 3. Automatic state saving/restoration helpers | |
*/ | |
abstract class ViewBindingBaseFragment<T : ViewBinding> : Fragment() { | |
private var _binding: T? = null | |
/** | |
* The binding instance that will be used to access views | |
* This property is only valid between onCreateView and onDestroyView | |
*/ | |
protected val binding: T | |
get() = _binding!! | |
/** | |
* Check if the binding is available | |
* This is useful for safely accessing binding in lifecycle methods like onSaveInstanceState | |
* where the view might have been destroyed | |
*/ | |
protected fun isBindingAvailable(): Boolean { | |
return _binding != null | |
} | |
/** | |
* Safely execute a block of code only if binding is available | |
* If binding is not available, the fallback block will be executed instead | |
* | |
* @param block The code to execute if binding is available | |
* @param fallback The code to execute if binding is not available (optional) | |
*/ | |
protected fun withBinding(block: (T) -> Unit, fallback: (() -> Unit)? = null) { | |
if (isBindingAvailable()) { | |
block(binding) | |
} else { | |
fallback?.invoke() | |
} | |
} | |
/** | |
* Safely execute a block of code that returns a value only if binding is available | |
* If binding is not available, the fallback value will be returned instead | |
* | |
* @param block The code to execute if binding is available | |
* @param fallbackValue The value to return if binding is not available | |
* @return The result of the block or the fallback value | |
*/ | |
protected fun <R> withBindingReturn(block: (T) -> R, fallbackValue: R): R { | |
return if (isBindingAvailable()) { | |
block(binding) | |
} else { | |
fallbackValue | |
} | |
} | |
/** | |
* Create the ViewBinding instance | |
* This method should be implemented by subclasses to provide the appropriate binding | |
*/ | |
abstract fun getViewBinding(inflater: LayoutInflater, container: ViewGroup?): T | |
override fun onCreateView( | |
inflater: LayoutInflater, | |
container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
_binding = getViewBinding(inflater, container) | |
return binding.root | |
} | |
override fun onDestroyView() { | |
super.onDestroyView() | |
_binding = null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment