본문 바로가기
Android

[Android] Landscape 시 알아둬야 할 점

by bryan.oh 2020. 3. 12.
반응형

Android Landscape

 

가로모드로 변경하도록 하는 방법이 두가지가 있는데

1. programmatically 하게 onCreate 에서 코드로 바꾸는 방법
2. manifest 에서 설정하는 방법

결론부터 말씀드리면

1번을 사용하면 어디에 해당 activity 가 다시 시작될 수 있습니다.
                                    (onCreate->onDestroy->onCreate)

2번은 별 이슈가 없습니다.


저의 경우엔 1번 방법을 사용했고

onDestroy 에서 thread 를 죽이는 작업을 하고있었는데 가끔식 오류가 발생하더라고요.

이 Activity 를 실행하면 onDestroy() 가 호출됩니다.


헐 뭥미..

아래와 같이 onCreate에 코드로 바꾸는 방법을 사용하면 화면이 다시 시작될 수 있다고 합니다

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_something);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        
        // .. 생략

 

안드로이드 개발자 페이지에 있는 내용입니다. 

Change the desired orientation of this activity. If the activity is currently in the foreground or otherwise impacting the screen orientation, the screen will immediately be changed (possibly causing the activity to be restarted). Otherwise, this will be used the next time the activity is visible.

번역

이 활동의 원하는 방향을 변경하십시오. 활동이 현재 포 그라운드에 있거나 화면 방향에 영향을주는 경우 화면이 즉시 변경됩니다 (활동이 다시 시작될 수 있음). 그렇지 않으면 다음에 활동이 표시 될 때 사용됩니다.

( activity 를 활동으로 번역하네요 )

 

세로모드로 시작했는데 onCreate 에서 가로모드로 바꾸니까 destroy 하고 다시 create 하는 모양입니다.

암튼 저와 같이 onDestroy 에서 thread 나 postdelay 같은 작업을 하면 꼬일 수 있으므로
아래와 같이 manifest 를 사용해야 합니다.

        <activity
            android:name="hello.bryan.SomeActivity"
            android:screenOrientation="landscape"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:theme="@style/FullScreenTheme" />

가로모드로 사용하려는 activity 에 screenOrientation 과 configChanges 를 추가합니다.

처음부터 가로모드로 실행하니 activity를 다시 시작할 필요가 없는거겠죠.

 

저의 삽질로 여러분은 삽질을 안하시길  > . <

 

 

728x90
반응형

댓글