[Android] instance

2020. 7. 3. 18:03·Android

 

when to use? 
  • Activity의 간단한 UI정보를 저장할 때
  • portrait <-> landscape (세로 <->가로) 모드 변경 시 

 

ex) 화면 모드 전환 Activity Lifecycle

[소멸과정]
onPause() ->onSaveInstanceState() -> onStop() -> onDestroy() 
[재생성 과정]
onCreate() -> onStart() ->onRestoreInstanceState(savedInstanceState : Bundle) -> onResume() 
새로 액티비티를 생성하는데 이때 기존에 액티비티가 가지고 있던 정보가 사라짐.
(ex. EditText)

 

onSaveInstanceState (outState : Bundle)

 

outState : Bundle in which to place your saved state. This value can't be null

 

 

 

onRestoreInstanceState(savedInstanceState : Bundle)

when is it called? this method is called between onStatrt() and onPostCreate(Bundle)

 

 

Example

//outState – Bundle in which to place your saved state.
//    user가 명시적으로 activity를 닫거나 finish()호출시 다음 메소드는 호출되지않음.
    override fun onSaveInstanceState(outState: Bundle) {
//    입력했던것 저장
        Log.i("State", "save Instance")
        outState.putString("USER_NAME", user_name_input_editText.text.toString())
        outState.putString("USER_PHONE", user_phone_number_editText.text.toString())
        super.onSaveInstanceState(outState)
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
        Log.i("State", "Restore Instance")
        super.onRestoreInstanceState(savedInstanceState)
        savedInstanceState?.run {
            user_name_input_editText.setText(savedInstanceState.getString("USER_NAME"))
            user_phone_number_editText.setText(savedInstanceState.getString("USER_PHONE"))
        }
    }

 

 

※ 뒤로가기 onBackPressed()같은 메소드 호출시 해당 Activity 객체는 메모리에서 해제되기 때문에

onSaveInstanceState를 해봤자 의미가 없다 .

∵ 다시 activity를 생성할 때는 새로운 activity 객체가 생성되기 때문이다.

[Reference]

https://developer.android.com/reference/android/app/Activity#onSaveInstanceState(android.os.Bundle)

저작자표시 (새창열림)

'Android' 카테고리의 다른 글

[Android] Activity to Activity Data Pass (feat. Intent)  (0) 2020.07.06
[Android] SharedPreference  (0) 2020.07.03
[Android] Service 2편 (Foreground)  (0) 2020.06.22
[Android] Service <-> Activity Communication (feat. Bind Service)  (0) 2020.06.21
[Android] Service (feat. Thread)  (0) 2020.06.20
'Android' 카테고리의 다른 글
  • [Android] Activity to Activity Data Pass (feat. Intent)
  • [Android] SharedPreference
  • [Android] Service 2편 (Foreground)
  • [Android] Service <-> Activity Communication (feat. Bind Service)
M_Falcon
M_Falcon
  • M_Falcon
    Falcon
    M_Falcon
  • 전체
    오늘
    어제
    • 분류 전체보기 (432)
      • Web (16)
        • Nodejs (14)
        • Javascript (23)
        • FrontEnd (4)
      • DataBase (39)
        • Fundamental (1)
        • Redis (4)
        • PostgreSQL (10)
        • NoSQL (4)
        • MySQL (9)
        • MSSQL (3)
        • Error (4)
      • Algorithm (79)
        • Algorithm (문제풀이) (56)
        • Algorithm (이론) (23)
      • JVM (65)
        • Spring (13)
        • JPA (5)
        • Kotlin (13)
        • Java (24)
        • Error (7)
      • 기타 (70)
        • Kafka (3)
        • Kubernetes (3)
        • Docker (13)
        • git (19)
        • 잡동사니 (27)
      • 재테크 (11)
        • 세무 (4)
        • 투자 (3)
        • 보험 (0)
      • BlockChain (2)
        • BitCoin (0)
      • C (32)
        • C (10)
        • C++ (17)
        • Error (3)
      • Low Level (8)
        • OS (3)
        • 시스템 보안 (5)
      • 네트워크 (3)
      • LINUX (30)
        • Linux (26)
        • Error (4)
      • 저작권과 스마트폰의 이해 (0)
      • 생각 뭉치 (6)
      • 궁금증 (2)
      • Private (4)
        • 이직 경험 (0)
        • 꿈을 찾아서 (1)
      • Android (21)
        • OS (4)
  • 블로그 메뉴

    • 홈
    • WEB
    • 알고리즘
    • DataBase
    • Linux
    • Mobile
    • C
    • 방명록
  • 링크

    • github
  • 공지사항

  • 인기 글

  • 태그

    Programmers
    android
    ubuntu
    linux
    알고리즘
    docker
    JPA
    kafka
    백준
    Spring
    database
    PostgreSQL
    java
    Kotlin
    C++
    프로그래머스
    Git
    javascript
    Bitcoin
    algorithm
  • hELLO· Designed By정상우.v4.10.3
M_Falcon
[Android] instance
상단으로

티스토리툴바