앱 만들기/안드로이드

안드로이드 화면 전환시 작업처리. onConfigurationChanged

나도 처음이야 2020. 4. 30.

 

 

안드로이드 가로 세로 화면 고정하기.

앱을 만들 때 폰의 방향을 바꾸어도, 가로/세로 한 방향으로만 UI 화면이 고정되도록 설계하는 경우가 있다. 굳이 양방향으로 보여줄 필요가 없을 때 말이다. 그럼 하기처럼 동작한다. 이럴 땐, 하기와 같은 간..

soo0100.tistory.com

지난 시간 화면 고정하는 방법에 이어서, 

화면 전환 과정에 대해서 조금 더 알아보자.

 

안드로이드에서 화면이 가로 혹은 세로 로 전환되면, 해당 Activity 의 Oncreate() 함수가 다시 불리게 된다.

즉, 화면을 지우고 새로 그리게 되는 것이다.

그런데, 화면이 전환 되더라도 Oncreate() 함수가 불리지 않게 하려면 어떻게 해야할까?

바로, AndroidManifest.xml 파일에 하기 음영 코드를 적용하면 된다.

즉, 화면 전화 및 화면의 사이즈가 변경될때는 Oncreate() 함수를 부르지 말라는 의미이다.

<activity android:name=".MyActivity"
         
android:configChanges="orientation|screenSize"
         
android:label="@string/app_name">

이외에도 여러가지 조건값들이 있다.

이는 안드로이드 개발자 문서를 참고 바란다.

https://developer.android.com/guide/topics/manifest/activity-element#config

 

<액티비티>  |  Android 개발자  |  Android Developers

Declares an activity (an Activity subclass) that implements part of the application's visual user interface. All activities must be represented by {@code } elements in the manifest file. Any that are not declared there will not be seen by the system…

developer.android.com

그런데, 해당 조건을 걸게되면 Oncreate() 함수 대신에 하기 onConfigurationChanged 함수가 호출된다.

개발자는 해당 함수를 오버라이딩 한뒤 가로,세로 화면에서 원하는 작업을 구현해주면 된다.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    
super.onConfigurationChanged(newConfig);

    
// Checks the orientation of the screen
    
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {


        
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();


    
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){


        
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();


    
}
}

 

 

구성 변경 처리  |  Android 개발자  |  Android Developers

일부 기기 구성은 런타임에 변경될 수 있습니다(예: 화면 방향, 키보드 가용성 및 사용자가 다중 창 모드를 활성화할 경우). 그러한 변경이 일어나는 경우 Android는 실행 중인 Activity를 다시 시작합니다(onDestroy()가 호출되고, 그다음에 onCreate()가 호출됨). 재시작 동작은 여러분이 제공한 대체 리소스로 애플리케이션을 자동으로 다시 로드함으로써 새로운 기기 구성에 애플리케이션이 적응하는 것을 돕도록 설계되었습니다(예: 다양한

developer.android.com

좋은 하루되세요.

감사합니다  :)

반응형

댓글