4

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

In tast 5 you simulate app shutdown and use 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.

The solution code from Codelab: https://github.com/google-developer-training/android-kotlin-fundamentals-apps/tree/master/DessertClickerFinal

And the MainActivity code

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

I use Android Studio and Kotlin. I shutdown the app using: adb shell am kill com.example.android.dessertclicker Or the terminate application button in the 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. 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?

  • 1
    please include all relevant code (just enough to describe your problem) in your question as text, so it creates an example for others to see what you're talking about exactly, the link you've posted could easily change and then your question becomes incomplete – a_local_nobody Jul 23 '20 at 08:12
  • Yes, good point I updated the question with the code – Emma Borkent Jul 23 '20 at 11:59
  • 1
    You are not alone: *[Android Kotlin Fundamentals codelab: save/restore bundle does not restore values because Bundle=null](https://github.com/google-developer-training/android-kotlin-fundamentals-apps/issues/283)*, *[Minimizing app with “home”, calls onSaveInstanceState, but when going to app again from “recent” state is lost](https://stackoverflow.com/questions/63314078)* – Alex Cohn Aug 12 '20 at 09:02

2 Answers2

3

I believe this rounds down to Can an activity be created with saved instance state saved by an older version of the app?. The system does not believe that the activity started from the Recents screen is a continuation of the activity launched from Android Studio. Maybe it's possible to advise it otherwise by forcing taskAffinity or launchMode for your activity, but nothing will be bullet-proof. You can reproduce the whole cycle from beginning if you start by running the app from the launcher, not from 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.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
0

try to include 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?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Felix
  • 284
  • 2
  • 12
  • 2
    I did read the Android Developers piece on lifecycles. When you put the app in the background Android might kill it, you can simulate this using the adb command mentioned in the post. Using Timber (or Log) I see that onSaveInstanceState _is called_ when I terminate the app in the background, but when starting the app from the recents screen, savedInstanceState is null. When I repeat this process right after, it is not null. So it seems the code is working, but why only on the second try? – Emma Borkent Jul 23 '20 at 12:07
  • have you add onRestoreInstanceState. I use it when I switch between activitys and want to store an restore the current states. – Felix Jul 23 '20 at 12:14
  • According to the developer documents you should be able to use both ways. But I tried and it is the same. onRestoreInstanceState is only called after I terminate the app for the second time – Emma Borkent Jul 23 '20 at 12:28
  • hmm, okay. I am out, i read the instructions from the game but i couldn't find a mistake... personally i never used it to restore information after killing an app, just to save states when i switch activitys... but one more: you create the information right like the instruction said: ". Press the cupcake at least five times until it switches to a donut." – Felix Jul 23 '20 at 13:12
  • Thank you Felix for your help. Did you run the solution code by any change? Just wondering if you experience the same behavior? In that case I could also send a "bug" report to Codelab – Emma Borkent Jul 24 '20 at 06:02
  • the codelab has already that [bug](https://github.com/google-developer-training/android-kotlin-fundamentals-apps/issues/241) but googlers seem doesn't care about it. – Arkaha Aug 13 '20 at 12:31