새로운 API 프록시
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
이 과정에서는 CompatTab
및 TabHelper
추상 클래스의 서브클래스를 만들고 새로운 API를 사용하는 방법을 보여줍니다. 애플리케이션은 이를 지원하는 플랫폼 버전을 실행하는 기기에서 이 구현을 사용할 수 있습니다.
새로운 API를 사용하여 탭 구현
최신 API를 사용하는 CompatTab
및 TabHelper
의 구체적인 클래스는 프록시 구현입니다. 이전 과정에서 정의된 추상 클래스가 새로운 API(클래스 구조, 메서드 서명 등)를 반영하므로 이러한 최신 API를 사용하는 구체적인 클래스는 메서드 호출과 그 결과를 간단히 프록시합니다.
느린 클래스 로드로 인해 이러한 구체적인 클래스에서 최신 API를 직접 사용할 수 있으며 이전 기기에서는 비정상 종료되지 않습니다. 클래스는 처음 액세스할 때 로드 및 초기화되어 처음으로 클래스를 인스턴스화하거나 클래스의 정적 필드 또는 메서드 중 하나에 액세스합니다. 따라서 Honeycomb 이전 기기에서 Honeycomb 특정 구현을 인스턴스화하지 않는 한 Dalvik VM은 VerifyError
예외를 발생시키지 않습니다.
이 구현의 올바른 이름 지정 규칙은 구체적인 클래스에 필요한 API와 일치하는 API 수준 또는 플랫폼 버전 코드명을 추가하는 것입니다. 예를 들어 네이티브 탭 구현은 CompatTabHoneycomb
및 TabHelperHoneycomb
클래스에서 제공할 수 있습니다. 이러한 클래스는 Android 3.0(API 수준 11) 이상에서 사용 가능한 API에 의존하기 때문입니다.
그림 1. 탭의 Honeycomb 구현 클래스 다이어그램
CompatTabHoneycomb 구현
CompatTabHoneycomb
은 TabHelperHoneycomb
이 개별 탭을 참조하는 데 사용하는 CompatTab
추상 클래스의 구현입니다. CompatTabHoneycomb
은 포함된 ActionBar.Tab
객체의 모든 메서드 호출을 간단히 프록시합니다.
다음과 같이 새 ActionBar.Tab
API를 사용하여 CompatTabHoneycomb
구현을 시작합니다.
Kotlin
class CompatTabHoneycomb internal constructor(val activity: Activity, tag: String) :
CompatTab(tag) {
...
// The native tab object that this CompatTab acts as a proxy for.
private var mTab: ActionBar.Tab =
// Proxy to new ActionBar.newTab API
activity.actionBar.newTab()
override fun setText(@StringRes textId: Int): CompatTab {
// Proxy to new ActionBar.Tab.setText API
mTab.setText(textId)
return this
}
...
// Do the same for other properties (icon, callback, etc.)
}
자바
public class CompatTabHoneycomb extends CompatTab {
// The native tab object that this CompatTab acts as a proxy for.
ActionBar.Tab mTab;
...
protected CompatTabHoneycomb(FragmentActivity activity, String tag) {
...
// Proxy to new ActionBar.newTab API
mTab = activity.getActionBar().newTab();
}
public CompatTab setText(int resId) {
// Proxy to new ActionBar.Tab.setText API
mTab.setText(resId);
return this;
}
...
// Do the same for other properties (icon, callback, etc.)
}
TabHelperHoneycomb 구현
TabHelperHoneycomb
은 포함된 Activity
에서 가져온 실제 ActionBar
의 메서드 호출을 프록시하는 TabHelper
추상 클래스의 구현입니다.
다음과 같이 TabHelperHoneycomb
을 구현하여 ActionBar
API 메서드 호출을 프록시합니다.
Kotlin
class TabHelperHoneycomb internal constructor(activity: FragmentActivity) : TabHelper(activity) {
private var mActionBar: ActionBar? = null
override fun setUp() {
mActionBar = mActionBar ?: mActivity.actionBar.apply {
navigationMode = ActionBar.NAVIGATION_MODE_TABS
}
}
override fun addTab(tab: CompatTab) {
// Tab is a CompatTabHoneycomb instance, so its
// native tab object is an ActionBar.Tab.
mActionBar?.addTab(tab.getTab() as ActionBar.Tab)
}
}
자바
public class TabHelperHoneycomb extends TabHelper {
ActionBar actionBar;
...
protected void setUp() {
if (actionBar == null) {
actionBar = activity.getActionBar();
actionBar.setNavigationMode(
ActionBar.NAVIGATION_MODE_TABS);
}
}
public void addTab(CompatTab tab) {
...
// Tab is a CompatTabHoneycomb instance, so its
// native tab object is an ActionBar.Tab.
actionBar.addTab((ActionBar.Tab) tab.getTab());
}
// The other important method, newTab() is part of
// the base implementation.
}
참조 사항
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[null,null,["최종 업데이트: 2025-07-27(UTC)"],[],[],null,["# Proxy to the new APIs\n\nThis lesson shows you how to subclass the `CompatTab` and `TabHelper` abstract classes and use new APIs. Your application can use this implementation on devices running a platform version that supports them.\n\nImplement tabs using new APIs\n-----------------------------\n\nThe concrete classes for `CompatTab` and `TabHelper` that use newer APIs are a *proxy* implementation. Since the abstract classes defined in the previous lesson mirror the new APIs (class structure, method signatures, etc.), the concrete classes that use these newer APIs simply proxy method calls and their results.\n\nYou can directly use newer APIs in these concrete classes---and not crash on earlier devices---because of lazy class loading. Classes are loaded and initialized on first access---instantiating the class or accessing one of its static fields or methods for the first time. Thus, as long as you don't instantiate the Honeycomb-specific implementations on pre-Honeycomb devices, the Dalvik VM won't throw any [VerifyError](/reference/java/lang/VerifyError) exceptions.\n\nA good naming convention for this implementation is to append the API level or platform version code name corresponding to the APIs required by the concrete classes. For example, the native tab implementation can be provided by `CompatTabHoneycomb` and `TabHelperHoneycomb` classes, since they rely on APIs available in Android 3.0 (API level 11) or later.\n\n**Figure 1.** Class diagram for the Honeycomb implementation of tabs.\n\nImplement CompatTabHoneycomb\n----------------------------\n\n`CompatTabHoneycomb` is the implementation of the `CompatTab` abstract class that `TabHelperHoneycomb` uses to reference individual tabs. `CompatTabHoneycomb` simply proxies all method calls to its contained [ActionBar.Tab](/reference/android/app/ActionBar.Tab) object.\n\nBegin implementing `CompatTabHoneycomb` using the new [ActionBar.Tab](/reference/android/app/ActionBar.Tab) APIs: \n\n### Kotlin\n\n```kotlin\nclass CompatTabHoneycomb internal constructor(val activity: Activity, tag: String) :\n CompatTab(tag) {\n ...\n // The native tab object that this CompatTab acts as a proxy for.\n private var mTab: ActionBar.Tab =\n // Proxy to new ActionBar.newTab API\n activity.actionBar.newTab()\n\n override fun setText(@StringRes textId: Int): CompatTab {\n // Proxy to new ActionBar.Tab.setText API\n mTab.setText(textId)\n return this\n }\n\n ...\n // Do the same for other properties (icon, callback, etc.)\n}\n```\n\n### Java\n\n```java\npublic class CompatTabHoneycomb extends CompatTab {\n // The native tab object that this CompatTab acts as a proxy for.\n ActionBar.Tab mTab;\n ...\n\n protected CompatTabHoneycomb(FragmentActivity activity, String tag) {\n ...\n // Proxy to new ActionBar.newTab API\n mTab = activity.getActionBar().newTab();\n }\n\n public CompatTab setText(int resId) {\n // Proxy to new ActionBar.Tab.setText API\n mTab.setText(resId);\n return this;\n }\n\n ...\n // Do the same for other properties (icon, callback, etc.)\n}\n```\n\nImplement TabHelperHoneycomb\n----------------------------\n\n`TabHelperHoneycomb` is the implementation of the `TabHelper` abstract class that proxies method calls to an actual [ActionBar](/reference/android/app/ActionBar), obtained from its contained [Activity](/reference/android/app/Activity).\n\nImplement `TabHelperHoneycomb`, proxying method calls to the [ActionBar](/reference/android/app/ActionBar) API: \n\n### Kotlin\n\n```kotlin\nclass TabHelperHoneycomb internal constructor(activity: FragmentActivity) : TabHelper(activity) {\n\n private var mActionBar: ActionBar? = null\n\n override fun setUp() {\n mActionBar = mActionBar ?: mActivity.actionBar.apply {\n navigationMode = ActionBar.NAVIGATION_MODE_TABS\n }\n }\n\n override fun addTab(tab: CompatTab) {\n // Tab is a CompatTabHoneycomb instance, so its\n // native tab object is an ActionBar.Tab.\n mActionBar?.addTab(tab.getTab() as ActionBar.Tab)\n }\n}\n```\n\n### Java\n\n```java\npublic class TabHelperHoneycomb extends TabHelper {\n ActionBar actionBar;\n ...\n\n protected void setUp() {\n if (actionBar == null) {\n actionBar = activity.getActionBar();\n actionBar.setNavigationMode(\n ActionBar.NAVIGATION_MODE_TABS);\n }\n }\n\n public void addTab(CompatTab tab) {\n ...\n // Tab is a CompatTabHoneycomb instance, so its\n // native tab object is an ActionBar.Tab.\n actionBar.addTab((ActionBar.Tab) tab.getTab());\n }\n\n // The other important method, newTab() is part of\n // the base implementation.\n}\n```\n\n### You should also read\n\n- [Action Bar](/guide/topics/ui/actionbar)\n- [Action Bar Tabs](/guide/topics/ui/actionbar#Tabs)"]]