devlog of ShinJe Kim

[Android] 프래그먼트에 기본생성자가 꼭 있어야하는 이유

|

프래그먼트에 기본생성자가 꼭 있어야하는 이유

All subclasses of Fragment must include a public no-argument constructor. The framework will often re-instantiate a fragment class when needed, in particular during state restore, and needs to be able to find this constructor to instantiate it. If the no-argument constructor is not available, a runtime exception will occur in some cases during state restore.

프래그먼트의 모든 서브클래스는 인자가 없는 생성자를 가져야만 합니다. 프레임워크는 종종 필요할 때 프래그먼트 클래스를 재인스턴스화하는데, 특히 상태(state)를 복구할 때 그렇습니다. 이 때 프래그먼트 클래스를 인스턴스화 하기 위해서는 빈 생성자가 필요합니다. 만약 빈 생성자를 사용할 수 없으면 상태 복구 도중에 런타임 익셉션이 발생할 수도 있습니다.

안드로이드 공식문서 - Fragment

즉, 안드로이드가 프래그먼트를 복원할 때 빈 생성자가 필요하고, 빈 생성자가 없으면 안드로이드 시스템이 프래그먼트를 생성할 수 없어 에러가 난다는 것이다.

(+빈 생성자가 없으면 안드로이드 시스템이 왜 프래그먼트를 생성할 수 없는지? 정리할 것)

Comments