개발

[Android] Don't Store Data in the Application Object

JUNIT74 2021. 11. 23. 19:28

http://www.developerphil.com/dont-store-data-in-the-application-object/

 

Don't Store Data in the Application Object

 

www.developerphil.com

 

요약하면 Activity간 공유되는 data를 Application class에 담을 수는 있지만 Application은 언제든지 OS에 의해 kill 될 수 있다.

가령 MyApplication -> Activity A -> Activity B 순으로 실행이 되었다고 가정했을 때

Activity A에서 MyApplication에 어떤 변수에 대한 값을 지정하고 Activity B로 이동한 이후 MyApplication이 kill되었고 다시 MyApplication이 실행 될 때 stack에 저장된 Activity B가 먼저 실행이 되므로 Activity A에서 MyApplication의 변수에 값을 지정하지 않아 Activity B에서는 해당 변수의 값을 얻을 수가 없게된다.

해당 글에서는 대체 방법을 아래와 명시적으로 Activity간 intent를 통하여 값을 전달하거나 storage를 이용하는 방법을 제시한다.

  • Explicitly pass the data to the Activity through the intent.
  • Use one of the many ways to persist the data to disk.
  • Always do a null-check and handle it manually.