활동 대상

탐색 그래프에서 대상은 활동일 수 있습니다. 앱에 단일 활동이 있는 것이 가장 좋지만 앱은 앱 내의 고유한 구성요소나 화면에 별도의 활동을 사용하는 경우가 많습니다. 이러한 경우에 활동 대상이 유용할 수 있습니다.

Compose 및 Kotlin DSL

탐색 그래프에 활동 대상을 추가하는 것은 Compose와 프래그먼트와 함께 Kotlin DSL을 사용할 때 모두 기본적으로 동일합니다. NavGraphNavHost 컴포저블에 전달할 때 동일한 createGraph() 람다를 사용하기 때문입니다.

자세한 내용은 Kotlin DSL을 사용하여 프로그래매틱 방식으로 그래프 빌드를 참고하세요.

XML

활동 대상을 만드는 방법은 프래그먼트 대상을 만드는 방법과 비슷합니다. 그러나 활동 대상의 특성은 상당히 다릅니다.

기본적으로 탐색 라이브러리NavControllerActivity 레이아웃에 연결하고 활성 탐색 그래프는 범위가 활성 Activity로 지정됩니다. 사용자가 다른 Activity로 이동하면 현재 탐색 그래프는 더 이상 범위에 포함되지 않습니다. 즉, Activity 대상은 탐색 그래프의 엔드포인트로 간주되어야 합니다.

활동 대상을 추가하려면 정규화된 클래스 이름으로 대상 Activity를 지정하세요.

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation_graph"
    app:startDestination="@id/simpleFragment">

    <activity
        android:id="@+id/sampleActivityDestination"
        android:name="com.example.android.navigation.activity.DestinationActivity"
        android:label="@string/sampleActivityTitle" />
</navigation>

이 XML은 다음 startActivity() 호출과 동일합니다.

Kotlin

startActivity(Intent(context, DestinationActivity::class.java))

Java

startActivity(new Intent(context, DestinationActivity.class));

이 접근법이 적합하지 않은 경우도 있습니다. 예를 들어 활동 클래스에 관한 컴파일 시간 종속 항목이 없거나 암시적 인텐트를 거치는 간접 수준을 선호할 수 있습니다. 대상 Activity의 매니페스트 항목에서 intent-filterActivity 대상을 구조화하는 방법을 지정합니다.

예를 들어 다음 매니페스트 파일을 살펴보겠습니다.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.navigation.activity">
    <application>
        <activity android:name=".DestinationActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <data
                    android:host="example.com"
                    android:scheme="https" />
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
</manifest>

상응하는 Activity 대상은 매니페스트 항목에 있는 속성과 일치하는 actiondata 속성으로 구성해야 합니다.

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation_graph"
    app:startDestination="@id/simpleFragment">
    <activity
        android:id="@+id/localDestinationActivity"
        android:label="@string/localActivityTitle"
        app:action="android.intent.action.VIEW"
        app:data="https://example.com"
        app:targetPackage="${applicationId}" />
</navigation>

targetPackage를 현재 applicationId로 지정하면 기본 앱을 포함하는 현재 애플리케이션으로 범위가 제한됩니다.

특정 앱을 대상으로 지정하려는 경우에도 이 메커니즘을 사용할 수 있습니다. 다음 예에서는 applicationIdcom.example.android.another.app인 앱을 대상으로 정의합니다.

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation_graph"
    app:startDestination="@id/simpleFragment">
    <activity
        android:id="@+id/localDestinationActivity"
        android:label="@string/localActivityTitle"
        app:action="android.intent.action.VIEW"
        app:data="https://example.com"
        app:targetPackage="com.example.android.another.app" />
</navigation>

동적 인수

이전 예에서는 대상으로 이동하는 데 고정 URL을 사용했습니다. 추가 정보가 URL의 일부로 전송되는 동적 URL을 지원해야 할 수도 있습니다. 예를 들어 https://example.com?userId=<actual user ID>와 유사한 형식의 URL로 사용자 ID를 보낼 수 있습니다.

이 경우 data 속성 대신 dataPattern를 사용합니다. 그런 다음 dataPattern 값에서 명명된 자리표시자로 대체할 인수를 제공합니다.

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation_graph"
    app:startDestination="@id/simpleFragment">
    <activity
        android:id="@+id/localDestinationActivity"
        android:label="@string/localActivityTitle"
        app:action="android.intent.action.VIEW"
        app:dataPattern="https://example.com?userId={userId}"
        app:targetPackage="com.example.android.another.app">
        <argument
            android:name="userId"
            app:argType="string" />
    </activity>
</navigation>

이 예에서는 Safe Args 또는 Bundle을 사용하여 userId 값을 지정할 수 있습니다.

Kotlin

navController.navigate(
    R.id.localDestinationActivity,
    bundleOf("userId" to "someUser")
)

Java

Bundle args = new Bundle();
args.putString("userId", "someUser");
navController.navigate(R.id.localDestinationActivity, args);

이 예에서는 someUser{userId}로 대체하고 https://example.com?userId=someUser의 URI 값을 만듭니다.