2010년 5월 3일 월요일

[android] Home example 배경 화면 유지 하기

example 중에 Home example을 보면 Launcher의 대략적인 구조를 알 수 있습니다.
그래서 Home을 보고 이것 저것 바꿔 가면서 공부 중인데 화면이 회전 되면 배경이 지원지는 문제가 발생 합니다. 그래서 portrait에서 landscape 전화 시 배경화면이 유지도록 수정 해 보겠습니다.


portrait에서 landscape 전화 시 배경이 지워짐.


기본 개념은 wallpaper을 얻어와 서 자신의 window의 background로 설정 하는 것입니다.

private void setDefaultWallpaper() {
if (!mWallpaperChecked) {
Drawable wallpaper = peekWallpaper();
if (wallpaper == null) {
try {
clearWallpaper();
} catch (IOException e) {
Log.e(LOG_TAG, "Failed to clear wallpaper " + e);
}
} else {
getWindow().setBackgroundDrawable(new ClippedDrawable(wallpaper));
}
mWallpaperChecked = true;
}
}

이유는 모르겠지만 화면이 회전이 되면 다시 setBackgroundDrawable()을 해줘야 합니다.

화면 회전 시 Activity의 lifecycle의 onPause() -> onResume이 발생합니다.

그래서 onResume() 함수를 override 후 아래와 같이 setBackgroundDrawable()을 해 주면 됩니다.

@Override
protected void onResume() {
super.onResume();
getWindow().setBackgroundDrawable(new ClippedDrawable(getWallpaper()));
bindRecents();
}



아주 간단하게 수정이 되었지만 이 부분을 찾는 데 많이 삽질 했습니다.
덕분에 Activity, Window, View의 큰 그림을 이해하게 되었네요.

댓글 없음: