ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Learn Android Programming How to build Android app using Kotlin

2022-03-20 10:37:50  阅读:243  来源: 互联网

标签:fragment Kotlin app user activity Android stage


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:
image

  • 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.
      image

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有