Android-Kotlin 探索 Activity的lifetime

若不清楚活动窗口的使用及其函数调用关系,相关函数功能就不能使用。

1 学习内容

为此按照官网课程进行学习和时间,主要掌握

activity 生命周期

  • activity 生命周期是 activity 会切换的一组状态。 activity 生命周期在 activity 首次创建时开始,到 activity 被销毁时结束。
  • 当用户在 activity 之间以及应用内外导航时,每个 activity 会在 activity 生命周期中的状态之间切换。
  • activity 生命周期中的每种状态都有一个对应的回调方法,您可以在 Activity 类中替换此类方法。核心的生命周期方法集合包括:onCreate()onStart()onPause()onRestart()onResume()onStop()onDestroy()
  • 如需添加要在 activity 转换为某种生命周期状态时发生的行为,请替换相应状态的回调方法。
  • 如需在 Android Studio 中为类添加框架替换方法,请依次选择 Code > Override Methods,或按 Control+o(在 Mac 上按 Command+o

使用日志进行记录

  • 借助 Android 日志记录 API(尤其是 Log 类),您可以写入要在 Android Studio 内的 Logcat 中显示的简短消息。
  • 使用 Log.d() 可写入调试消息。此方法采用两个参数:日志标签(通常是类的名称)和日志消息(一个简短的字符串)。
  • 使用 Android Studio 中的 Logcat 窗口可查看系统日志,包括您写入的消息

采用的app界面如下,由于主要是观测activity的各函数,因此app的函数不介绍

2 Codes

   添加监测函数, 方法在 MainAcitivity 用Ctr+O 可以 打开 函数,shift选择 onCreate()onStart()onPause()onRestart()onResume()onStop()onDestroy() 进行加载。

增加监测函数

日志消息的优先级,即消息的重要性。在本示例中,Log.d() 方法会写入调试消息。
Log 类中的其他方法包括 Log.i()(表示信息性消息)、Log.e()(表示错误)、Log.w()(表示警告)或 Log.v()(表示详细消息)。
日志标签(第一个参数),在本示例中为 "MainActivity"。该标签是一个字符串,可让您更轻松地在 Logcat 中找到自己的日志消息。该标签通常是类的名称。
实际的日志消息(第二个参数)是一个简短的字符串,在本示例中为 "onCreate called"。
注意:一种比较好的做法是,在类中声明 TAG 常量:

const val TAG = "MainActivity"

然后在后续对日志方法的调用中使用该名称,如下所示:

Log.d(TAG, "onCreate Called")

    override fun onStart() {

        super.onStart()
        Log.d(tag,"OnStart Called") // it will show oncreate called in Logcat
    }

    /**
     * 当 activity 从头开始启动时,您会看到系统按顺序调用以下三个生命周期回调:

    onCreate():用于创建应用。
    onStart():用于启动相应 activity,并让其在屏幕上显示。
    onResume():用于使相应 activity 成为焦点,并让用户能够与其互动。
     */


    override fun onResume() {
        super.onResume()
        Log.d(tag,"OnResume Called") // it will show oncreate called in Logcat
    }

    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        super.onSaveInstanceState(outState, outPersistentState)
        outState.putInt(KEY_REVENUE,revenue)//通过 putInt() 方法将 revenue 值(整数)放入 bundle 中
        outState.putInt(KEY_DESSERT_SOLD, dessertsSold)

        Log.d(tag,"OnSave Instance Called") // it will show oncreate called in Logcat
    }

    override fun onPause() {
        super.onPause()
        Log.d(tag,"OnPause Called") // it will show oncreate called in Logcat
    }

    override fun onStop() {
        super.onStop()
        Log.d(tag,"OnStop Called") // it will show oncreate called in Logcat
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(tag,"OnDestroy Called") // it will show oncreate called in Logcat
    }

这样用Log.d增加的日志监测函数,可以实现动作检测。

新东西: 在xml中可以定义变量,在控件中就能使用。 我是半路出家,所以这个trick get 了。

    <data>

        <variable
            name="revenue"
            type="Integer" />

        <variable
            name="amountSold"
            type="Integer" />
    </data>


         <TextView
            android:id="@+id/revenue_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:text='@{"$" + revenue.toString()}'  
            android:textColor="@color/green"
            android:textSize="@dimen/large_text_size"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            tools:text="$92" />

3 Results

 但是没有监测到 

onSaveInstanceState() ?

查看了一下,官网的解释更具体

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

做一个码农都是奢望

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值