라이프 사이클 종류
resumed: 앱이 화면에 다시 보이기 시작하는 경우, 유저의 인풋에 반응할 수 있다.
inactive: 앱이 비활성화되고 유저의 인풋에 반응할 수 없다.
paused: 앱이 유저에게 보이지 않고, 유저의 인풋에 반응할 수 없으며 백그라운드로 동작한다. (보통 inactive 이후 실행)
detached(suspending): 모든 뷰가 제거되고 플러터 엔진만 동작 중이며 앱이 종료되기 직전에 실행된다. 해당 상황은 앱을 스와이프로 제거하거나, 배터리가 부족해서 종료될 때, 메모리 부족으로 스왑될 경우 동작한다. 이 경우 보통 100%의 관찰을 보장하지 않는다
1. WidgetsBindingObserver불러오기
class _tsetState extends State<ChatPage> with WidgetsBindingObserver {
...
}
2. init에 옵저버 등록
@override
void initState() {
super.initState();
WidgetsBinding.instance?.addObserver(this);
}
3. override하기
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
switch(state){
case AppLifecycleState.resumed:
break;
case AppLifecycleState.inactive:
break;
case AppLifecycleState.detached:
break;
case AppLifecycleState.paused:
break;
}
}