Skip to content

Instantly share code, notes, and snippets.

@sahara-ooga
Last active January 3, 2020 06:01
Show Gist options
  • Save sahara-ooga/234859e2441dfab4b06f2ff20f0c00ef to your computer and use it in GitHub Desktop.
Save sahara-ooga/234859e2441dfab4b06f2ff20f0c00ef to your computer and use it in GitHub Desktop.
Androidのライフサイクルメソッドの呼び出しを検証した

Android Activity Lifecycle methods

Log

起動、AppSwitcher、ホームボタン、アプリ強制終了

## 起動

com.example.android.dessertpusher I/MainActivity: onCreate Called
com.example.android.dessertpusher I/MainActivity: onStart called
com.example.android.dessertpusher I/MainActivity: onResume called

## App Switcherに移動

com.example.android.dessertpusher I/MainActivity: onPause called
com.example.android.dessertpusher I/MainActivity: onStop called

## 再びForegroundへ

com.example.android.dessertpusher I/MainActivity: onRestart called
com.example.android.dessertpusher I/MainActivity: onStart called
com.example.android.dessertpusher I/MainActivity: onResume called

## ホームボタンを押して、バックグラウンドへ

com.example.android.dessertpusher I/MainActivity: onPause called
com.example.android.dessertpusher I/MainActivity: onStop called

## 再びForegroundへ

com.example.android.dessertpusher I/MainActivity: onRestart called
com.example.android.dessertpusher I/MainActivity: onStart called
com.example.android.dessertpusher I/MainActivity: onResume called

## AppSwitcherから、プロセス終了

com.example.android.dessertpusher I/MainActivity: onPause called
com.example.android.dessertpusher I/MainActivity: onStop called
com.example.android.dessertpusher I/MainActivity: onDestroy called

起動し、Backボタンを押下

# 起動
com.example.android.dessertpusher I/MainActivity: onCreate Called
com.example.android.dessertpusher I/MainActivity: onStart called
com.example.android.dessertpusher I/MainActivity: onResume called

# Backボタンを押下
com.example.android.dessertpusher I/MainActivity: onPause called
com.example.android.dessertpusher I/MainActivity: onDestroy called
com.example.android.dessertpusher I/MainActivity: onStop called

implementation

Timberを使用した。

class MainActivity : AppCompatActivity(), LifecycleObserver {
    /** Lifecycle Methods **/
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Timber.i("onCreate Called")
    }

    override fun onStart() {
        super.onStart()
        Timber.i("onStart called")
    }

    // TODO (05) Here, override the rest of the lifecycle methods and use Timber to print
    // log statements. Don't forget to update the log statement in onCreate!

    /**
     * Dispatch onResume() to fragments.  Note that for better inter-operation
     * with older versions of the platform, at the point of this call the
     * fragments attached to the activity are *not* resumed.  This means
     * that in some cases the previous state may still be saved, not allowing
     * fragment transactions that modify the state.  To correctly interact
     * with fragments in their proper state, you should instead override
     * [.onResumeFragments].
     */
    override fun onResume() {
        super.onResume()
        Timber.i("onResume called")
    }

    /**
     * Dispatch onPause() to fragments.
     */
    override fun onPause() {
        super.onPause()
        Timber.i("onPause called")
    }

    override fun onDestroy() {
        super.onDestroy()
        Timber.i("onDestroy called")
    }

    override fun onRestart() {
        super.onRestart()
        Timber.i("onRestart called")
    }

    override fun onStop() {
        super.onStop()
        Timber.i("onStop called")
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment