3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Activity,Fragmentのライフサイクル/Android/Kotlin

Last updated at Posted at 2019-06-11

MainActivity.kt

あとでAPILevelも追加したい
https://developer.android.com/guide/components/activities.html?hl=ja#Lifecycle

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // The activity is being created.
        println("Activity/onCreate/アクティビティ作成")
    }

    override fun onStart() {
        super.onStart()
        // The activity is about to become visible.
        println("Activity/onStart/アプリをスタート")
    }

    override fun onPostCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onPostCreate(savedInstanceState, persistentState)
        println("Activity/onPostCreate//普段使わない")
    }

    override fun onResume() {
        super.onResume()
        // The activity has become visible (it is now "resumed").
        println("Activity/onResume/アクティビティの実行")
    }

    override fun onPostResume() {
        super.onPostResume()
        println("Activity/onPostResume//普段使わない")
    }

    override fun onUserLeaveHint() {
        super.onUserLeaveHint()
        println("Activity/onUserLeaveHint//普段使わない")
    }

    override fun onSaveInstanceState(outState: Bundle?, outPersistentState: PersistableBundle?) {
        super.onSaveInstanceState(outState, outPersistentState)
        // ここでアクティビティの状態を保存する。
        // !!アクティビティが破棄される前に onSaveInstanceState() が呼び出される保証はありません
        // これは、状態を保存する必要がないケースがあるためです
        // (ユーザーが [Back] ボタンを使用してアクティビティを離れることで明示的にアクティビティを閉じた場合など)
        println("Activity/onSaveInstanceState/onstop or/普段使わない")
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
        super.onRestoreInstanceState(savedInstanceState)
        // API Level 28
        println("Activity/onRestoreInstanceState")
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        println("Activity/onWindowFocusChanged/アプリを閉じた時")
    }

    override fun onPause() {
        super.onPause()
        // Another activity is taking focus (this activity is about to be "paused").
        // ユーザーセッション後も保存する必要がある変更点を保存(別のアクティビティに引き継ぐ等)
        println("Activity/onPause/別のアクティビティを開く時、非表示にする")
    }

    override fun onCreateThumbnail(outBitmap: Bitmap?, canvas: Canvas?): Boolean {
        // API Level 28
        println("Activity/onCreateThumbnail/いつ使うんだろう")
        return super.onCreateThumbnail(outBitmap, canvas)
    }

    override fun onStop() {
        super.onStop()
        // The activity is no longer visible (it is now "stopped")
        println("Activity/onStop/アクティビティが消えた時")
    }

    override fun onDestroy() {
        super.onDestroy()
        // The activity is about to be destroyed.
        println("Activity/onDestroy/アクティビティの終了")
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        println("Activity/onNewIntent/アクティビティRestartの前")
    }

    override fun onRestart() {
        super.onRestart()
        println("Activity/onRestart/アクティビティを再表示")
    }

MainActivityFragment.kt

    override fun onAttach(context: Context?) {
        super.onAttach(context)
        println("Fragment/onAttach/")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        println("Fragment/onCreate/")
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return super.onCreateView(inflater, container, savedInstanceState)
        println("Fragment/onCreateView/")
    }
    
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        println("Fragment/onActivityCreated/")
    }

    override fun onStart() {
        super.onStart()
        println("Fragment/onStart/")
    }

    override fun onResume() {
        super.onResume()
        println("Fragment/onResume/")
    }

    override fun onPause() {
        super.onPause()
        println("Fragment/onPause/")
    }

    override fun onStop() {
        super.onStop()
        println("Fragment/onStop/")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        println("Fragment/onDestroyView/")
    }

    override fun onDestroy() {
        super.onDestroy()
        println("Fragment/onDestroy/")
    }

    override fun onDetach() {
        super.onDetach()
        println("Fragment/onDetach/")
    }


ios版

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?