티스토리 뷰

PrivacyParcelable.kt

package com.jwsoft.kotlinproject

import android.os.Parcel
import android.os.Parcelable

data class PrivacyParcelable (
    var height: Int,
    var weight: Int
) : Parcelable {

    constructor(parcel: Parcel) : this(
        parcel.readInt(),
        parcel.readInt()
    )

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeInt(height)
        parcel.writeInt(weight)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<PrivacyParcelable> {
        override fun createFromParcel(parcel: Parcel): PrivacyParcelable {
            return PrivacyParcelable(parcel)
        }

        override fun newArray(size: Int): Array<PrivacyParcelable?> {
            return arrayOfNulls(size)
        }
    }

}

 

 

 

 

 

PrivacySerializable.kt

package com.jwsoft.kotlinproject

import java.io.Serializable

class PrivacySerializable (
    var height: Int,
    var weight: Int
) : Serializable

 

 

 

 

 

MainActivity.kt

package com.jwsoft.kotlinproject

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.e("", "onCreate()")

        if (savedInstanceState != null) {
            Log.e("", savedInstanceState.getString("name").toString())
            Log.e("", savedInstanceState.getInt("age").toString())

            var privacyParcelable = savedInstanceState.getParcelable<PrivacyParcelable>("privacyParcelable")
            if (privacyParcelable != null) {
                Log.e("", privacyParcelable.height.toString() + " " + privacyParcelable.weight.toString())
            }

            var privacySerializable = savedInstanceState.getSerializable("privacySerializable") as PrivacySerializable
            if (privacySerializable != null) {
                Log.e("", privacySerializable.height.toString() + " " + privacySerializable.weight.toString())
            }
        }
    }

    override fun onStart() {
        super.onStart()
        Log.e("", "onStart()")
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        Log.e("", "onRestoreInstanceState()")
    }

    override fun onResume() {
        super.onResume()
        Log.e("", "onResume()")
    }

    override fun onPause() {
        super.onPause()
        Log.e("", "onPause()")
    }

    override fun onStop() {
        super.onStop()
        Log.e("", "onStop()")
    }

    override fun onSaveInstanceState(outState: Bundle) {
        Log.e("", "onSaveInstanceState()")
        outState.putString("name", "James Kim")
        outState.putInt("age", 30)
        outState.putSerializable("privacySerializable", PrivacySerializable(180, 80))
        outState.putParcelable("privacyParcelable", PrivacyParcelable(180, 80))
        super.onSaveInstanceState(outState)
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e("", "onDestroy()")
    }

}

실행 결과

 

회전 결과

 

 

 

onSaveInstanceState() 함수가 onStop() 함수 이후에 호출됐는데 해당 프로젝트의 타겟 버전이 29이기 때문이다.

29버전 이전으로 사용하게 되면 onStop() 함수가 호출되기 이전에 onSaveInstanceState() 함수가 호출되는데

onPause() 함수 이전에 호출될지 이후에 호출될지는 보장할 수 없다.

 

 

 

 

참고.

stackoverflow.com/questions/20831826/when-exactly-are-onsaveinstancestate-and-onrestoreinstancestate-called

 

When exactly are onSaveInstanceState() and onRestoreInstanceState() called?

The following figure (from the official doc) describes the well-known lifecycle of an Android activity: On the other hand, when the activity is destroyed by the system (for example because memory ...

stackoverflow.com

 

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함