230814 TIL - splash 화면 (Kotlin)

장재용·2023년 8월 14일

TIL

목록 보기
20/32

Splash 화면은 앱을 구동시켰을 때 메인화면이 나오기 전 잠깐 나오는 화면을 말한다.

총 세가지 작업을 해줘야한다.

🎯 xml 화면 구성
간단하게 로고만 뛰울거면 이미지뷰만 넣어주면 된다.

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".StartActivity">


    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="260dp"
        android:layout_height="331dp"
        android:layout_marginTop="84dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_logo_15ya" />
    
    
</androidx.constraintlayout.widget.ConstraintLayout>


🎯 Activity.kt에 핸들러 작성

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.start_activity)


        Handler().postDelayed({
            val intent = Intent(this, LoginActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
            startActivity(intent)
            finish()
        },2000)

    }

위 2000부분을 화면에 뛰우고자 하는 시간으로(밀리세컨드 단위기에 1초는 1000) 설정해주면 된다.

처음 시작되는 Activity 바꾸기

AndroidManifest.xml 파일을 열어서
첫 시작으로 설정할 Activity에

        <activity
            android:name=".StartActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

위와 같이 android:exported="true"로 바꾸고 (기존 false)
intent-filter를 추가하면 된다.

또 첫 시작에서 바뀌어버린 후발주자 Activity는

   <activity
            android:name=".MainActivity"
            android:exported="false" />

위와 같이 intent-filter 태그는 삭제하고 exported:false로 수정해주면 된다.

profile
enjoy_error_message!

0개의 댓글