Fragment
This codebase comes with its' own Base Fragment which you should use for all your Fragment
. To use the base Fragment just inherit Fragment
to DevFragment
:
class SampleFragment : DevFragment<FragmentSampleBinding>(){
...
}
class SampleFragment : DevFragment(){
...
}
Abstract Method¶
When inheriting Fragment
to DevFragment
, there's methods to override to group code:
initData
- define variable initialization. e.g:late init
variable or data from intentinitUI
- define UI configuration. e.g: settings adapter forRecyclerView
or setting text inTextView
initAction
- define actions or behavior in yourActivity
. e.g: hitting APIinitObserver
- define process of observeMutableLiveData
Important
It is important that you should always group your code inside all of this method, so it's maintainable
Features¶
After using DevFragment
, these are method that are ready to use.
setupToolbar¶
It's said in Initial Configuration that Activity
does not come with embedded Toolbar
. So always define Toolbar
in xml layout file. hen, call this method to set toolbar with Fragment
:
setupToolbar(
toolbar = tbMain, // the toolbar to be defined
title = getStringResource(string.labelToolbarTitle), // title of the toolbar
isChild = false, // display the back button on the toolbar?
menu = R.menu.menu_main, // menu resource for the toolbar
onMenuListener = { // listener when menu item is clicked
when (it) {
R.id.menuAdd -> {
startActivity(Intent(this, FormActivity::class.java))
}
R.id.menuCamera -> {
startActivity(Intent(this, CameraActivity::class.java))
}
R.id.menuError -> throw Exception("Does this help?")
}
false
}
)
requestPermissionsSafely¶
Does the apps need a certain Permission? Do that by calling this method:
requestPermissionsSafely(
// array of permissions to request
permissions = arrayOf(
Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
),
// request code to assign for this request
requestCode = 3001,
// action when permission granted
onPermissionGranted = { pvCamera.post { startCamera() } },
// action when permission is not granted
onPermissionNotGranted = {
displayToast(this, getStringResource(string.permission_camera_not_granted))
onBackPressed()
})
hasPermission and hasPermissions¶
Check does the app has permissions by calling this method:
// for one permission
val permissionGranted = hasPermission(Manifest.permission.CAMERA)
// for more than one permission
val permissionsGranted = hasPermissions(arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE))
Picking Image¶
When picking image, for example setting up user profile. Call one of these method:
// Pick image from gallery
startPickImageFromGallery(requestCode)
// Pick image from camera
startPickImageFromCamera(requestCode)
onActivityResult
method to get the image like this: override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == IF_GALLERY) {
val selectedImage: Uri? = data?.data
} else if (requestCode = IF_CAMERA) {
val selectedImage : Bitmap? = data?.extras?.get("data") as Bitmap
}
}