본문

180402(월) - Android Architecture Components (Saving UI States)

Android Architecture Components 


Saving UI States

- UI state를 preserve하거나 하지 않는것은 user experience에 중요한 부분

- user expects state를 maintain 하는것이 중요하다.


- simple and lightweight UI data는 onSaveInstanceState()를 사용

- complex data는 ViewModel, onStateInstanceState(), persistent local storage combination 사용


Managing simpler cases: onSaveInstanceState()

- designed small amounts of data reload the state of UI controller

- 이 callback은 다음 두가지 상황을 처리하기 위한 것

1. memory constraint로 인해서 kills the app's process

2. configuration change (screen rotation, change input language...)

- invoke activity is stopped, but not finished by system

- explicitly close or finish() called, onSaveInstanceState()는 not called

- overriding을 통해서 custom data 저장도 가능


- large amounts of data (bitmap, complex data structures, lengthy serialization or deserialization)를 저장하도록 design되지는 않음

- serialized object가 복잡하면 많은 memory를 소비하게 된다.

- 이 process는 configuration change 동안에 main thread에서 실행되기때문에 serialization을 너무 오래하면 dropped frame and visual stutter

- 그러므로 complex data structures 저장시에는 local persistent storage를 대신 사용

- data가 create되자마자 store 해야 lose를 최소화 할 수 있으며, onSaveInstanceState()를 사용해서 이 objects의 unique IDs를 저장

https://developer.android.com/topic/libraries/architecture/room.html


Managing more complex states: divide and conquer

- 보존해야할 complex data structures가 있다면 dividing several types storage mechanisms에 의해서 효과적으로 UI state를 save and restore를 할 수 있다.

- user can leave activity general way and outcomes

1. user completely closes the activity

- 최근 사용 앱(recents screen) 으로 날려버리는 경우

- user는 다시 시작할때 clean state로 시작하기를 예상함


2. rotate or background

- 이전에 보던 화면 및 data가 그대로 있기를 기대함


- Local persistence & ViewModel & onSaveInstanceState()를 사용

- Local persistence

lose를 원하지 않는 모든 data를 stores


- ViewModel

UI controller관련된 모든 data를 memory에 store


- onSaveInstanceState() 

small amount data stores.

complex object는 local storage에 저장하고, 이러한 object에 대한 unique IDs를 저장


Restoring complex states: reassembling the pieces

- possible scenarios

1. activity가 system에 의해서 stopped 되었다가 recreated

onSaveInstanceState()에 저장된 query를 가지고 ViewModel에 넘긴 후 search cached and delegates -> 결과적으로 없음


2. activity create configuration change

onSaveInstanceState()에 저장된 query를 가지고 ViewModel에 넘긴 후 data loading. (not need re-query the database)


어쨌든 ViewModel을 사용하면 database에서 re-load data 해야하는 wasting cycles를 avoid 가능

공유

댓글