Guest User

Untitled

a guest
Aug 14th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. class CustomLifecycleOwner(private val context: Context) : LifecycleOwner {
  2.  
  3. private val TAG = "AUDIT 2"
  4. private var auditHelper: AuditHelperLifecycleAware? = null
  5. private var lifecycle: LifecycleRegistry = LifecycleRegistry(this)
  6.  
  7. init {
  8. lifecycle.markState(Lifecycle.State.INITIALIZED)
  9. onCreate()
  10. }
  11.  
  12. private fun onStart(){
  13. lifecycle.markState(Lifecycle.State.STARTED)
  14. onResume()
  15. }
  16.  
  17. private fun onCreate(){
  18.  
  19. var action1: (String) -> Unit = {
  20. Log.i(TAG, String.format(context.getString(R.string.report_from_custom_lifecycle_owner) , it))
  21. }
  22.  
  23. var action2: (String) -> Unit = {
  24. var dialog = AlertDialog.Builder(context)
  25. with(dialog){
  26. setPositiveButton(context.getString(R.string.ok_button), null)
  27. setMessage(String.format(context.getString(R.string.report_action) , it))
  28. show()
  29. }
  30. }
  31.  
  32. auditHelper = AuditHelperLifecycleAware(lifecycle,action1, action2)
  33. lifecycle.addObserver(auditHelper!!)
  34.  
  35. lifecycle.markState(Lifecycle.State.CREATED)
  36. onStart()
  37. }
  38.  
  39. private fun onDestroy(){
  40. lifecycle.markState(Lifecycle.State.DESTROYED)
  41. }
  42.  
  43.  
  44. private fun onResume(){
  45. lifecycle.markState(Lifecycle.State.RESUMED)
  46. onDestroy()
  47. }
  48.  
  49.  
  50. @NonNull
  51. override fun getLifecycle(): Lifecycle {
  52. return lifecycle
  53. }
  54. }
Add Comment
Please, Sign In to add comment