Learn Android Programming How to build Android app using Kotlin
作者:互联网
All icons were sourced from here and are free for commercial use with attribution
-
The smartphone icon used on the book cover was made by Freepik.
-
The phone dial pad icons used in the Communication application were made by Pixel Buddha.
-
The carrot, broccoli and strawberry icons used for demonstration products in the Store application were made by Icongeek26.
-
The play, pause, skip forward and skip backward playback control icons used in the Music application were made by Elias Bikbulatov.
Getting started
An introduction to Kotlin and Android programming
- The applications covered in this article are powered by a programming language called Kotlin.
-
Kotlin was developed by a company called JetBrains.
-
The first stable release became available in 2016.
-
While Kotlin was released relatively recently, it is heavily based on an older programming language called Java.
-
Kotlin and Java are interoperable, which means you can use both languages in the same project.
-
Q:Why use Kotlin if it is so similar to Java?
A:In short, Kotlin is concise, expressive and safe. Kotlin creates powerful applications that perform well and are easy to maintain. The benefits of Kotlin are such that in 2019 Google announced that Kotlin is the preferred programming language for Android applications.
For now, it is sufficient to understand the following key concepts:
-
Kotlin is an object-oriented programming language.
class Recipe{ }
-
Variables can take various forms
-
If the value of a variable is fixed then it is initialised using the val keyword.
-
if the value can be changed then it is initialised using the var keyword.
-
variable values can be null.
-
It is convention to write variable names using camel case.
- As myFirstVariable
-
-
Primitive data types
-
Char - A single character.
-
String - Text such as a word or sentence.
-
Integer - A whole number. Can be negative, positive or 0.
-
Float - A number containing decimal places.
-
Boolean - A true/false value.
-
-
Kotlin classes can store the instructions for completing a given task inside a method.
val tempDifference =temperatureDifference(80, 200) fun temperatureDifference(currentTemperature: Int, targetTemperature: Int): Int { val difference = targetTemperature - currentTemperature return difference }
The lifecycle of Android activities and fragments:
-
An activity is amodule that serves a role within the application.
-
A fragment is a section of an activity that features a uniqueuser interface.
-
An activity can use zero or multiple fragments.
-
Activities are reusable and can be sharedbetween applications.
At runtime, an activity will cycle between several states in a sequence called the Android activity lifecycle, as summarised in the below diagram:
- onCreate:The onCreate stage is where user interface components are initialised and any data operations required to set up the activity are performed.
-
onStart:The onStart stage, where the user interface becomes visible.
-
onResume:The onStart stage is followedby the onResume stage once the activity is ready to handle user interactions. The activity will then occupy the system foreground for as long as it is visible to the user. The foreground activity is prioritised when the device allocates system memory and computational processing power.
-
onPause:If an activity loses its foreground status, like when the user closes the application or another activity occupies the foreground, the losing activity will enter the onPause stage of its lifecycle.
-
onStop:The onPause stage will transition to the onStop stage when the activity is no longer visible.
At either the onPause stage or the onStop stage, the activity can reenter the foreground state if the user returns to the activity.
An important consideration for activities that enter the onPause and onStop stages is that those activities may be killed if there is insufficient memory to keep them running in the background.
-
onRestar: If the user returns to the activity when it is in the onStop stage, then the activity must progress through a stage called onRestart before the activity can become visible again.
-
onDestory stage:if the activity is closed and the user does not return, then the activity will progress to the onDestroy stage. The onDestroy stage handles any procedures that should be carried out before the activity is destroyed.
As mentioned previously:
-
An activity can comprise multiple fragments.
-
Each fragment contains a lifecycle of its own, albeit directly interrelated with the activity lifecycle.
-
If the fragment’s parent activity closes then the fragment will shut down also.
-
The fragment lifecycle shares many stages with the activity lifecycle; however, the fragment lifecycle does not include the onRestart stage.
-
The fragment lifecycle contains several extra stages relating to the fragment’s user interface view, as summarised below:
-
onCreateView():The fragment is loading.
-
onViewCreated():The user interface layout is now available.
-
onDestroyView():The fragment is shutting down.
-
Installing Android Studio
Android Studio is the official development environment for building and testing Android apps and is available for Windows, macOS and Linux operating systems.
Download Android Studio fromAndroid’s website
标签:fragment,Kotlin,app,user,activity,Android,stage 来源: https://www.cnblogs.com/tlo-liangjiajia/p/16005673.html