설정 구성 Android Jetpack의 구성요소

설정 화면이 크고 복잡하면 사용자가 변경하려는 특정 설정을 찾기 어려울 수 있습니다. 환경설정 라이브러리는 설정 화면을 더 효과적으로 구성할 수 있도록 다음과 같은 방법을 제공합니다.

환경설정 카테고리

한 화면에 관련된 Preference 객체가 여러 개 있다면 PreferenceCategory를 사용하여 그룹화할 수 있습니다. PreferenceCategory는 카테고리 제목을 표시하고 카테고리를 시각적으로 구분합니다.

XML에서 PreferenceCategory을 정의하려면 다음과 같이 Preference 태그를 PreferenceCategory로 래핑합니다.

<PreferenceScreen
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <PreferenceCategory
        app:key="notifications_category"
        app:title="Notifications">

        <SwitchPreferenceCompat
            app:key="notifications"
            app:title="Enable message notifications"/>

    </PreferenceCategory>

    <PreferenceCategory
        app:key="help_category"
        app:title="Help">

        <Preference
            app:key="feedback"
            app:summary="Report technical issues or suggest new features"
            app:title="Send feedback"/>

    </PreferenceCategory>

</PreferenceScreen>

결과는 다음과 같습니다.

카테고리가 포함된 환경설정을 보여주는 이미지
그림 1. 카테고리 내 환경설정

계층 구조를 여러 개의 화면으로 분할

Preference 객체 또는 고유한 카테고리가 많으면 별도의 화면에 표시할 수 있습니다. 각 화면은 별도의 계층 구조가 있는 PreferenceFragmentCompat입니다. 그러면 초기 화면의 Preference 객체가 관련 환경설정이 포함된 하위 화면에 연결될 수 있습니다.

그림 2는 메시지동기화라는 두 개의 카테고리를 포함하는 간단한 계층 구조를 보여줍니다.

계층 구조가 있는 환경설정 화면을 보여주는 이미지
그림 2. 두 개의 카테고리가 있는 간단한 계층 구조

그림 3은 동일한 환경설정 세트를 여러 화면으로 분할한 것을 보여줍니다.

여러 화면으로 분할된 계층 구조를 보여주는 이미지
그림 3. 여러 화면으로 분할된 계층 구조

화면을 Preference에 연결하려면 XML에 app:fragment를 선언하거나 Preference.setFragment()를 사용하면 됩니다. 다음 예와 같이 Preference를 탭하면 PreferenceFragmentCompat의 전체 패키지 이름을 실행합니다.

<Preference
        app:fragment="com.example.SyncFragment"
        .../>

사용자가 연결된 Fragment가 있는 Preference를 탭하면 인터페이스 메서드인 PreferenceFragmentCompat.OnPreferenceStartFragmentCallback.onPreferenceStartFragment()가 호출됩니다. 이 메서드에서 새 화면 표시를 처리하고 주변 Activity에 화면이 구현되는 위치를 처리합니다.

일반적인 구현은 다음과 유사합니다.

Kotlin

class MyActivity : AppCompatActivity(),
    PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
    ...
    override fun onPreferenceStartFragment(caller: PreferenceFragmentCompat, pref: Preference): Boolean {
        // Instantiate the new Fragment.
        val args = pref.extras
        val fragment = supportFragmentManager.fragmentFactory.instantiate(
                classLoader,
                pref.fragment)
        fragment.arguments = args
        fragment.setTargetFragment(caller, 0)
        // Replace the existing Fragment with the new Fragment.
        supportFragmentManager.beginTransaction()
                .replace(R.id.settings_container, fragment)
                .addToBackStack(null)
                .commit()
        return true
    }
}

Java

public class MyActivity extends AppCompatActivity implements
        PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
    ...
    @Override
    public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
        // Instantiate the new Fragment.
        final Bundle args = pref.getExtras();
        final Fragment fragment = getSupportFragmentManager().getFragmentFactory().instantiate(
                getClassLoader(),
                pref.getFragment());
        fragment.setArguments(args);
        fragment.setTargetFragment(caller, 0);
        // Replace the existing Fragment with the new Fragment.
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.settings_container, fragment)
                .addToBackStack(null)
                .commit();
        return true;
    }
}

PreferenceScreens

중첩된 &lt;PreferenceScreen&gt;를 사용하여 동일한 XML 리소스 내에서 중첩된 계층 구조를 선언하는 것은 더 이상 지원되지 않습니다. 대신 중첩된 Fragment 객체를 사용하세요.

별도의 활동 사용

또는, 각 화면에 맞춤설정할 것이 많거나 화면 간에 전체 Activity 전환을 한다면 각 PreferenceFragmentCompat에 별도의 Activity를 사용할 수 있습니다. 이렇게 하면, 각 Activity 및 이에 상응하는 설정 화면을 완전히 맞춤설정할 수 있습니다. 대부분의 앱에는 이 방법을 권장하지 않습니다. 대신 앞에서 설명한 대로 Fragments를 사용합니다.

Preference에서 Activity를 실행하는 방법에 관한 자세한 내용은 환경설정 작업을 참고하세요.