简体   繁体   English

savedInstanceState 是 null 在第一次应用程序关闭后但不是在第二次

[英]savedInstanceState is null after first app shutdown but not after second

I am following this codelab Android Kotlin Fundamentals 04.2: Complex lifecycle situations https://codelabs.developers.google.com/codelabs/kotlin-android-training-complex-lifecycle/index.html#4我正在关注这个代码实验室 Android Kotlin 基础知识 04.2:复杂的生命周期情况https://codelabs.developers.google.com/codelabs-koindex/android

In tast 5 you simulate app shutdown and use onSaveInstanceState()在 tast 5 中,您模拟应用程序关闭并使用 onSaveInstanceState()

When I follow the instructions, but also when I run the solution code, saveInstanceState is not restored after the first app shutdown , but only after the second app shutdown .当我按照说明进行操作时,以及在运行解决方案代码时,第一个应用程序关闭后并没有恢复 saveInstanceState ,而是在第二个应用程序关闭后才恢复。

The solution code from Codelab: https://github.com/google-developer-training/android-kotlin-fundamentals-apps/tree/master/DessertClickerFinal来自 Codelab 的解决方案代码: https://github.com/google-developer-training/android-kotlin-fundamentals-apps/tree/master/DessertClickerFinal

And the MainActivity code和 MainActivity 代码

class MainActivity : AppCompatActivity() {

    private var revenue = 0
    private var dessertsSold = 0
    private lateinit var dessertTimer : DessertTimer;
    private var currentDessert = allDesserts[0]

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        Timber.i("onCreate called")

        if (savedInstanceState != null) {
            revenue = savedInstanceState.getInt(KEY_REVENUE, 0)
            dessertsSold = savedInstanceState.getInt(KEY_DESSERT_SOLD, 0)
            dessertTimer.secondsCount =
                    savedInstanceState.getInt(KEY_TIMER_SECONDS, 0)
            // Show the next dessert
            showCurrentDessert()
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Timber.i("onSaveInstanceState Called")
        outState.putInt(KEY_REVENUE, revenue)
        outState.putInt(KEY_DESSERT_SOLD, dessertsSold)
        outState.putInt(KEY_TIMER_SECONDS, dessertTimer.secondsCount)
    } 

In the solution code project, I do the following:在解决方案代码项目中,我执行以下操作:

  • Run the app, click on the dessert (to create data to save)运行应用程序,点击甜点(创建数据保存)
  • Put app to background, kill it将应用程序置于后台,将其杀死
  • Open app with recents screen使用最近屏幕打开应用程序
  • onSaveInstanceState is null.. onSaveInstanceState 是 null..
  • Put the app in the backgroud again, kill it再次将应用程序置于后台,将其杀死
  • Open app with recents screen使用最近屏幕打开应用程序
  • savedState is restored已恢复已保存状态

This happens every time;每次都会发生这种情况; After the app is running and shutdown for the first time, nothing happens, after I kill it for the second time, savedState is restored.应用程序第一次运行和关闭后,没有任何反应,在我第二次杀死它后,savedState 恢复了。

I use Android Studio and Kotlin.我使用 Android Studio 和 Kotlin。 I shutdown the app using: adb shell am kill com.example.android.dessertclicker Or the terminate application button in the Logcat.我使用以下命令关闭应用程序: adb shell am kill com.example.android.dessertclicker或 Logcat 中的终止应用程序按钮。 Both give the same result.两者都给出相同的结果。

I read that some programmers say you have to call super.onSaveInstanceState after putting the key value pairs to outstate:Bundle, but I just followed the sample code.我读到一些程序员说你必须在将键值对放到 outstate:Bundle 之后调用 super.onSaveInstanceState,但我只是按照示例代码进行的。 When I try to call it after, I get the same result anyway.当我尝试调用它时,无论如何我都会得到相同的结果。

Does anyone have any clue why this happens?有谁知道为什么会这样? And can anyone tell me if they experience the same behavior when running the solution code?谁能告诉我他们在运行解决方案代码时是否遇到相同的行为?

I believe this rounds down to Can an activity be created with saved instance state saved by an older version of the app?我相信这可以归结为可以使用旧版本的应用程序保存的已保存实例 state 创建活动吗? . . The system does not believe that the activity started from the Recents screen is a continuation of the activity launched from Android Studio.系统不认为从 最近屏幕启动的活动是从 Android Studio 启动的活动的延续。 Maybe it's possible to advise it otherwise by forcing taskAffinity or launchMode for your activity, but nothing will be bullet-proof .也许可以通过强制taskAffinitylaunchMode为您的活动提供建议,但没有什么是防弹的 You can reproduce the whole cycle from beginning if you start by running the app from the launcher, not from Android Studio.如果从启动器而不是 Android Studio 运行应用程序,则可以从头开始重现整个周期。

Also, it's important to remember:此外,重要的是要记住:

Note : 注意

Multiple tasks can be held in the background at once.可以同时在后台执行多个任务。 However, if the user is running many background tasks at the same time, the system might begin destroying background activities in order to recover memory, causing the activity states to be lost.但是,如果用户同时运行多个后台任务,系统可能会开始破坏后台活动以恢复 memory,从而导致活动状态丢失。

try to include onRestoreInstanceState.尝试包括 onRestoreInstanceState。 read more about the lifecycle here .此处阅读有关生命周期的更多信息。 But I dont think it save the state if you kill the app... maybe you should better use SharedPreferences ?但我认为如果你杀死应用程序,它不会保存 state ......也许你应该更好地使用SharedPreferences

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 返回活动后,savedInstanceState为null - savedInstanceState null after returning to activity OS杀死活动后,片段savedInstanceState不为null - Fragment savedInstanceState not null after activity killed by OS 活动销毁后,savedInstanceState始终为null - savedInstanceState is always null after activity gets destroyed 设备轮换后,savedInstanceState 包始终为 null - savedInstanceState bundle is always null after device rotation 为什么在应用程序处于后台时更改设备语言后,SavedInstanceState 在活动中不是 null? - Why SavedInstanceState is not null in activity after changing device language while the app is in background? 多次更改方向后,savedInstanceState中的值将变为null - Value in savedInstanceState becomes null after multiple orientation changes 从第二个应用程序返回后,在第一个应用程序上打印“ toast”消息 - printing a “toast” message on the first app after returning from the second app onCreate(savedInstanceState)savedInstanceState为NULL - onCreate(savedInstanceState) savedInstanceState is NULL 应用仅适用于第一个输入,如果插入第二个输入,则崩溃 - App only works with first input, crashes after if second input is inserted 重新启动应用程序后,活动第二次findViewById()调用返回null - Activity second findViewById() call returns null after app restart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM