안드로이드 개발을 하다보면 사용자 편의에 따라 가로모드, 세로모드를 지원해야 할때가 있다.
역으로 게임의 경우 가로모드만 지원할 수도 있고, 쇼핑몰의 경우 세로모드만 지원해야 할 때가 있다.
이를 정리 해 보고자 한다.
1. 안드로이드에서 화면 회전 및 고정 방법
1) 가로모드 고정 (LandScape)
- 앱을 실행하면 가로로 실행된다. 또는 특정 액티비티는 가로로만 출력된다.
- XML 에서 넣는 방법 (AndroidManifest.xml, screenOrientation)
<application
android:name=".app.MyApplication"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
...
android:screenOrientation="landscape"
...
/>
- 코드로 넣는 방법 (Target-Activity, setRequestedOrientation)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
...
}
2) 세로모드 고정 (Portrait)
- 앱을 실행하면 가로로 실행된다. 또는 특정 액티비티는 가로로만 출력된다.
- XML 에서 넣는 방법 (AndroidManifest.xml, screenOrientation)
<application
android:name=".app.MyApplication"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
...
android:screenOrientation="portrait"
...
/>
- 코드로 넣는 방법 (Target-Activity, setRequestedOrientation)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
...
}
3) 사용자 취사 선택 허용
- 사용자가 디바이스를 회전할때마다 화면이 회전할 수 있도록 제공.
- 그러나 앱 정보가 초기화 되지 않도록 할때
<application
android:name=".app.MyApplication"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
...
android:configChanges="orientation|keyboard|screenSize|keyboardHidden"
...
/>
<activity
2. 안드로이드 화면 회전 생명주기
1) 화면 회전시 생명주기 VS 일반 액티비티 생명주기
- onCreate 부터 다시 실행하므로 동일하다.
- 위 1-3 configChanges 를 적용하였을 경우에는 onCreate 를 실행하지 않는다. 참고 1)
- 직접 실행해보면 알겠지만, 회전시 onConfigurationChanged 를 실행함을 알 수 있다.
3. 안드로이드 화면 회전에 따른 처리
*) 사용자 회전에 대한 처리
- 일반적으로 Activity 에 onConfigurationChanged 메소드를 재정의하여 사용한다.
@Override
public void onConfigurationChanged(Configuration newConfig) {
if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
// TODO : 세로 모드 일때
}
else if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
// TODO : 가로 모드 일때
}
super.onConfigurationChanged(newConfig);
}
-- 주의 ) 구성설정(Configuration) 은 ORIENTATION_LANDSCAPE 하고 ORIENTATION_PORTRAIT 이외에도 언디파인드, 또는 회전과 관계없는 정보가 해당 메소드를 호출할 수 있으므로 이를 유의하며 작성해야 한다.
참고 1 : https://developer.android.com/guide/topics/resources/runtime-changes?hl=ko
구성 변경 처리 | Android Developers
일부 기기 구성은 런타임에 변경될 수 있습니다(예: 화면 방향, 키보드 가용성 및 사용자가 다중 창 모드를 활성화할 경우). 그러한 변경이 일어나는 경우 Android는 실행 중인 Activity를 다시 시작합니다(onDestroy()가 호출되고, 그다음에 onCreate()가 호출됨). 재시작 동작은 여러분이 제공한 대체 리소스로 애플리케이션을 자동으로 다시 로드함으로써 새로운 기기 구성에 애플리케이션이 적응하는 것을 돕도록 설계되었습니다(예: 다양한
developer.android.com
'programming > java, Spring, android, js' 카테고리의 다른 글
[Android] Obsolete configuration warning (0) | 2019.11.07 |
---|---|
HTTP Status 500 – Internal Server Error, Mapped Statements collection does not contain value (0) | 2019.11.04 |
[Spring boot+Quartz] 배치 샘플 (0) | 2019.10.18 |
[Java] Binary to Byte File (0) | 2019.06.05 |
[Android] Key 분실, JKS 재생성 및 PEM 재배포/업로드 방법 (0) | 2019.06.04 |