Guest User

Untitled

a guest
Aug 14th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. package com.example.marcos.lifecycleawarecomponents
  2.  
  3. import android.support.v7.app.AppCompatActivity
  4. import android.os.Bundle
  5. import android.support.v7.app.AlertDialog
  6. import android.util.Log
  7. import kotlinx.android.synthetic.main.activity_main.*
  8.  
  9. class MainActivity : AppCompatActivity() {
  10.  
  11. private var auditHelper: AuditHelper? = null
  12. private val TAG = "AUDIT"
  13.  
  14. override fun onCreate(savedInstanceState: Bundle?) {
  15. super.onCreate(savedInstanceState)
  16. setContentView(R.layout.activity_main)
  17.  
  18.  
  19. /**
  20. * Action that we want to execute when the user start, pause and stop the app.
  21. * To keep the example single we are just logging the message, but this can be a request to server,
  22. * database operation, etc.
  23. * This action is send as parameter to the AuditHelper class
  24. */
  25. var action1: (String) -> Unit = {
  26. Log.i(TAG, String.format(getString(R.string.report_from_main_activity) , it))
  27. }
  28.  
  29.  
  30. /**
  31. * Action to execute if a custom operation is done by the user, in this case we are showing a dialog
  32. * but it could be a complex operations
  33. * This action is send as parameter to the AuditHelper class
  34. */
  35. var action2: (String) -> Unit = {
  36. var dialog = AlertDialog.Builder(this)
  37. with(dialog){
  38. setPositiveButton(getString(R.string.ok_button), null)
  39. setMessage(String.format(getString(R.string.report_from_main_activity) , it))
  40. show()
  41. }
  42.  
  43. }
  44.  
  45. auditHelper = AuditHelper(action1, action2)
  46.  
  47. //Set listener for the buttons
  48. btn_action1.setOnClickListener { auditHelper?.reportAction(String.format(getString(R.string.report_action) , 1))}
  49. btn_action2.setOnClickListener { auditHelper?.reportAction(String.format(getString(R.string.report_action) , 2))}
  50. btn_action3.setOnClickListener { auditHelper?.reportAction(String.format(getString(R.string.report_action) , 3)) }
  51. }
  52.  
  53.  
  54. override fun onStart() {
  55. /**
  56. * We report when the app starts
  57. */
  58. auditHelper?.auditStarted()
  59. super.onStart()
  60. }
  61.  
  62. override fun onPause() {
  63. /**
  64. * Report when the app is paused
  65. */
  66. auditHelper?.auditPause()
  67. super.onPause()
  68. }
  69.  
  70. override fun onStop() {
  71. /**
  72. * Report when the app is stopped
  73. */
  74. auditHelper?.auditStop()
  75. super.onStop()
  76. }
  77. }
Add Comment
Please, Sign In to add comment