Android typically used XML layout files to define the user interface. Kotlin code then works behind to load the layout for each activity, fragment, etc. Essentially we are connecting the visual to the program side. All visual elements are objects and we can assign variables such as id and properties.

Activity Lifecycle

Activity is a page in an Android Application. Other elements like buttons, text elements, progress bars, etc. are contained in this activity. Consider it as a base on which you construct your application.

Override onCreate()

MainActivity.kt class is the first screen or activity to appear when the user launches the app and linked to activity_main.xml. We are able to automatically access the variables to the visual side as priority is given to the code during execution.

onCreate() is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.

Function or Method in Kotlin

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function.
Functions are used to perform certain actions, and they are also known as methods. A typical method in Kotlin appears to have a fun keyword, method name, formal parameters (arguments) and return type.

Example of a method

MainActivity.kt

MethodDescription
onStart()check if a user has already signed in to your app with Google. Else sign in anonymously
savePreferenceSignIn()Shared preferences allow you to store small amounts of primitive data as key/value pairs in a file on the device. To get a handle to a preference file, and to read, write, and manage preference data, use the SharedPreferences class.
onCreateOptionsMenu()called by the Android runtime when it need to create the option menu
onActivityResult()when you done with the subsequent activity and returns, the system calls your activity’s onActivityResult() method. This method includes three arguments:

(1) The request code you passed to startActivityForResult().
(2) result code specified by the second activity. This is either RESULT_OK if the operation was successful or RESULT_CANCELED if the operation failed
(3) An Intent that carries the result data.
showDownloadDialog()inflate a dialog box for fetching game
showTimeDialog()inflate a dialog for timer setting
setTimeToMemory()storing new timer to sharedpreferences
setupSponsorBanner()setup a sponsor banner and display any message or link provided by sponsor
downloadGame()fetch the desirable game from Firebase
updateFirebaseCounter()increment counter each time game is fetched from Firebase
updateFirebaseRecord()updates new time recorded
showCreationDialog()inflate a dialog box for creating new card game
showNewSizeDialog()inflate a dialog box for board size selection
showReplayDialog()inflate a dialog box for replaying game
startCountDownTimer()timer begin counting down upon game starts
onFinish()call showReplayDialog() upon game finishes
startBlink()animate the timer during game
stopTimer()cancel time counter
setupBoard()setup the board game upon app starts or game ends
formatTime()time formatter
updateGameWithFlip()flip card at position upon clicked
Methods in class MainActivity.kt
class MainActivity : AppCompatActivity() {

    companion object {

        private const val TAG = "MainActivity"
        private const val CREATE_REQUEST_CODE = 248
        private const val GAME_REQUEST_CODE = 173
        private const val SPONSOR_CODE = 199
        private const val SPONSOR_SECTION_CODE = 118

    }
Read more