Activity
This codebase comes with its' own Base Activity which you should use for all your Activity
. To use the base Activity just inherit Activity
to DevActivity
:
class SampleActivity : DevActivity<ActivitySampleBinding>(){
...
}
class SampleActivity : DevActivity(){
...
}
Abstract Method¶
When inheriting Activity
to DevActivity
, 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 DevActivity
, 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. Then, call this method to set toolbar with Activity
:
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
pickImageFromGallery { uri ->
// Do something with your uri image
}
// Pick image from camera
pickImageFromCamera(uriForImage) { uri ->
// Do something with the image!
}
setFragment¶
Set Fragment
to View
using this method:
setFragment(
R.id.someView, // the view Id where fragment to be inflated
theFragment, // the fragment to inflate
true // add this fragment to backstack?
)
opening another Activity¶
for a simple opening Activity, call this method:
start<YourActivityClass>(isFinished = false)