0

Learning this google codelab I faced with such a problem - no state restore, even in solution code I tried few emulators, and live devices with 6 and 9 android versions, but no luck.

Steps I do:

  • Open app
  • Press "home"
  • Call adb command to emulate app kill by system
  • Press "recent"
  • Choose my app
  • In most cases, I see lost state

ADB command used in a codelab: adb shell am kill $APP_PACKAGE_NAME

I found that even I have waited for 10 seconds after "home" before calling ADB command, the state can be lost(in most cases), but if I go after "home" to any other application, and call ADB command after that, in most cases my app can restore state.

The log from the app, where I try to save variable SOME_DATA

I/StateLoose: onCreate hasBundle=false
I/StateLoose: onSaveInstanceState has state: Bundle[{SOME_DATA=Sat Aug 08 17:09:40 GMT+08:00 2020, android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@bb196, 2131165191=androidx.appcompat.widget.Toolbar$SavedState@51a8117, 2131165193=android.view.AbsSavedState$1@bb196, 2131165199=android.view.AbsSavedState$1@bb196, 2131165241=android.view.AbsSavedState$1@bb196}}], android:lastAutofillId=1073741823, android:fragments=android.app.FragmentManagerState@e682904}]

calling adb command, and restoring from recent:

I/StateLoose: onCreate hasBundle=false

The simplified activity code:

const val name: String = "SOME_DATA"

class StateLoose : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_state_loose)
        Timber.i("onCreate hasBundle=${null != savedInstanceState}")
        savedInstanceState?.run {
            Timber.i("data ${getString(name, "")}")
        }
    }

    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        outState?.run {
            putString(name, Date().toString())
        }
        Timber.i("onSaveInstanceState has state: $outState")
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
        super.onRestoreInstanceState(savedInstanceState)
        Timber.i("onRestoreInstanceState ${null != savedInstanceState}")
        savedInstanceState?.run {
            Timber.i("data ${getString(name, "")}")
        }
    }
}

So my questions:

  1. Why the state is lost in some cases?
  2. How not to lose the state?
Arkaha
  • 1,582
  • 3
  • 17
  • 19
  • 2
    looks as a duplicate of *[savedInstanceState is null after first app shutdown but not after second](https://stackoverflow.com/questions/63049536)*, which has no upvoted answers yet. – Alex Cohn Aug 12 '20 at 08:58
  • Yep, it's the same problem... – Arkaha Aug 13 '20 at 12:28
  • 1
    OK, I believe I have the [answer](https://stackoverflow.com/a/63375053/192373) for you: it's because of *Android Studio*! It's natural that when you press Shift-F10, the app starts from $0. But also if you launch the app from device, it does not count the instance that was started from AS. – Alex Cohn Aug 15 '20 at 19:21
  • 1
    I checked 10 times, without AS launch, and seems you are right, 10 times in a row I have state restored... Any idea on how to fix that? – Arkaha Aug 16 '20 at 03:33
  • Fix what? It's part of delicate mechanics of Android Studio which allows live updates to a running app. The big question is, why should we care? Is running the app from AS a real-life scenario where you need the app state restored? – Alex Cohn Aug 16 '20 at 07:07
  • For example to debug the restore mechanism? – Arkaha Aug 16 '20 at 09:49
  • *to debug the restore mechanism* No problem, just start the app 'naturally' and attach the debugger. See *[how to use “Select debug app” and “wait for debugger”?](https://stackoverflow.com/q/11947640/192373)* – Alex Cohn Aug 16 '20 at 09:55

0 Answers0