Cannot trust current state of AAC lifecycle

223 Views Asked by At

Issue summary

getLifecycle().getCurrentState() gave me wrong state.

Environment (integrated library, OS, etc)

  • compileSdkVersion : 27
  • targetSdkVersion : 27
  • support library : 27.1.1

Expected behavior

  • onCreate() -> CREATED
  • onStart() -> STARTED
  • onResume() -> RESUMED
  • onPause() -> STARTED
  • onStop() -> CREATED
  • onDestroy() -> DESTROYED

Actual behavior

  • onCreate : INITIALIZED
  • onStart : CREATED
  • onResume : STARTED
  • onPause : STARTED
  • onStop : CREATED
  • onDestroy : DESTROYED

Issue detail

I tried using below code. I searched google but no one thinks strangely about this. Am I something missing?

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.i(MainActivity::class.java.name, "onCreate : " + lifecycle.currentState.name)
    }

    override fun onStart() {
        super.onStart()
        Log.i(MainActivity::class.java.name, "onStart : " + lifecycle.currentState.name)
    }

    override fun onResume() {
        super.onResume()
        Log.i(MainActivity::class.java.name, "onResume : " + lifecycle.currentState.name)
    }

    override fun onPause() {
        super.onPause()
        Log.i(MainActivity::class.java.name, "onPause : " + lifecycle.currentState.name)
    }

    override fun onStop() {
        super.onStop()
        Log.i(MainActivity::class.java.name, "onStop : " + lifecycle.currentState.name)
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i(MainActivity::class.java.name, "onDestroy : " + lifecycle.currentState.name)
    }
}
1

There are 1 best solutions below

0
On

Your actual behaviour is the expected behaviour if you look at the documentation: https://developer.android.com/topic/libraries/architecture/lifecycle

lifecycle

Think of the states as nodes of a graph and events as the edges between these nodes.