Last active
February 3, 2023 12:48
-
-
Save axiel7/91933f78f5b8f2e7762f4a6220161a36 to your computer and use it in GitHub Desktop.
Android BaseActivity with ViewBinding
This file contains 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 androidx.appcompat.app.AppCompatActivity | |
import androidx.core.view.WindowCompat | |
import androidx.viewbinding.ViewBinding | |
abstract class BaseActivity<VB: ViewBinding> : AppCompatActivity() { | |
private var _binding: VB? = null | |
protected val binding get() = _binding!! | |
protected abstract val bindingInflater: (LayoutInflater) -> VB | |
override fun onCreate(savedInstanceState: Bundle?) { | |
//onPrecreate() | |
super.onCreate(savedInstanceState) | |
WindowCompat.setDecorFitsSystemWindows(window, false) // draw behind system bars | |
_binding = bindingInflater.invoke(layoutInflater) | |
setContentView(binding.root) | |
setup() | |
} | |
/** | |
* Called after setContentView in onCreate | |
*/ | |
abstract fun setup() | |
override fun onDestroy() { | |
super.onDestroy() | |
_binding = null | |
//onDestroying() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment