static context에서 리소스 컨텐트를 어떻게 얻을수 있을까요?
조회수 3170회
1 답변
-
- Application의 하위 클래스를 만들어줍니다. public class App extends Application{ 같은
- 안드로이드 매니페스트 파일에서 의 태그에 android:name속성의 값에 ".App"를 대입합니다.
- onCreate()메소드에서 static으로 선언된 context 객체에 this 값을 넣어줍니다. 그리고 그 값을 얻어올수있는 getApp()같은 static메소드를 정의합니다.
public class App extends Application{ private static Context mContext; @Override public void onCreate() { super.onCreate(); mContext = this; } public static Context getContext(){ return mContext; } }
이렇게 하시면 App.getContext() 메소드를 써서 언제든지 context값을 받아올수있습니다. 그리고 App.getContext().getResource()로 getResource의 값을 받아올수있습니다.
댓글 입력