নতুন API-এর প্রক্সি
সেভ করা পৃষ্ঠা গুছিয়ে রাখতে 'সংগ্রহ' ব্যবহার করুন
আপনার পছন্দ অনুযায়ী কন্টেন্ট সেভ করুন ও সঠিক বিভাগে রাখুন।
এই পাঠটি আপনাকে দেখায় কিভাবে CompatTab
এবং TabHelper
বিমূর্ত ক্লাসগুলি সাবক্লাস করতে হয় এবং নতুন API ব্যবহার করতে হয়। আপনার অ্যাপ্লিকেশানটি তাদের সমর্থন করে এমন একটি প্ল্যাটফর্ম সংস্করণ চলমান ডিভাইসগুলিতে এই বাস্তবায়ন ব্যবহার করতে পারে৷
নতুন API ব্যবহার করে ট্যাব প্রয়োগ করুন
CompatTab
এবং TabHelper
এর জন্য কংক্রিট ক্লাস যা নতুন API ব্যবহার করে একটি প্রক্সি বাস্তবায়ন। যেহেতু পূর্ববর্তী পাঠে সংজ্ঞায়িত বিমূর্ত ক্লাসগুলি নতুন APIs (শ্রেণির কাঠামো, পদ্ধতি স্বাক্ষর, ইত্যাদি) প্রতিফলিত করে, তাই এই নতুন APIগুলি ব্যবহার করে এমন কংক্রিট ক্লাসগুলি কেবল প্রক্সি পদ্ধতি কল এবং তাদের ফলাফলগুলি।
অলস ক্লাস লোডিংয়ের কারণে আপনি এই কংক্রিট ক্লাসগুলিতে সরাসরি নতুন API ব্যবহার করতে পারেন—এবং আগের ডিভাইসগুলিতে ক্র্যাশ হবে না৷ প্রথম অ্যাক্সেসে ক্লাসগুলি লোড করা হয় এবং আরম্ভ করা হয়—ক্লাসটি ইনস্ট্যান্টিয়েটিং বা প্রথমবারের জন্য এর একটি স্ট্যাটিক ক্ষেত্র বা পদ্ধতি অ্যাক্সেস করা। এইভাবে, যতক্ষণ না আপনি প্রাক-হানিকম্ব ডিভাইসগুলিতে হানিকম্ব-নির্দিষ্ট বাস্তবায়নগুলিকে ইনস্ট্যান্টিয়েট না করেন, ডালভিক ভিএম কোনও VerifyError
ব্যতিক্রম ফেলবে না।
এই বাস্তবায়নের জন্য একটি ভাল নামকরণ কনভেনশন হল কংক্রিট ক্লাসগুলির জন্য প্রয়োজনীয় APIগুলির সাথে সংশ্লিষ্ট API স্তর বা প্ল্যাটফর্ম সংস্করণ কোড নাম যুক্ত করা। উদাহরণস্বরূপ, নেটিভ ট্যাব বাস্তবায়ন CompatTabHoneycomb
এবং TabHelperHoneycomb
ক্লাসগুলি দ্বারা সরবরাহ করা যেতে পারে, যেহেতু তারা Android 3.0 (API স্তর 11) বা তার পরে উপলব্ধ APIগুলির উপর নির্ভর করে৷

চিত্র 1. ট্যাবগুলির মৌচাক বাস্তবায়নের জন্য ক্লাস ডায়াগ্রাম।
CompatTabHoneycomb প্রয়োগ করুন
CompatTabHoneycomb
হল CompatTab
অ্যাবস্ট্রাক্ট ক্লাসের বাস্তবায়ন যা TabHelperHoneycomb
পৃথক ট্যাবগুলিকে উল্লেখ করতে ব্যবহার করে। CompatTabHoneycomb
এর মধ্যে থাকা ActionBar.Tab
অবজেক্টে সমস্ত মেথড কল প্রক্সি করে।
নতুন ActionBar.Tab
API ব্যবহার করে CompatTabHoneycomb
প্রয়োগ করা শুরু করুন:
কোটলিন
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
হল TabHelper
অ্যাবস্ট্রাক্ট ক্লাসের বাস্তবায়ন যা প্রক্সি মেথড একটি প্রকৃত ActionBar
কল করে, এটির অন্তর্ভুক্ত Activity
থেকে প্রাপ্ত।
TabHelperHoneycomb
প্রয়োগ করুন, ActionBar
API-তে প্রক্সি পদ্ধতি কল করুন:
কোটলিন
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.
}
আপনারও পড়া উচিত
এই পৃষ্ঠার কন্টেন্ট ও কোডের নমুনাগুলি Content License-এ বর্ণিত লাইসেন্সের অধীনস্থ। Java এবং OpenJDK হল Oracle এবং/অথবা তার অ্যাফিলিয়েট সংস্থার রেজিস্টার্ড ট্রেডমার্ক।
2025-07-29 UTC-তে শেষবার আপডেট করা হয়েছে।
[[["সহজে বোঝা যায়","easyToUnderstand","thumb-up"],["আমার সমস্যার সমাধান হয়েছে","solvedMyProblem","thumb-up"],["অন্যান্য","otherUp","thumb-up"]],[["এতে আমার প্রয়োজনীয় তথ্য নেই","missingTheInformationINeed","thumb-down"],["খুব জটিল / অনেক ধাপ","tooComplicatedTooManySteps","thumb-down"],["পুরনো","outOfDate","thumb-down"],["অনুবাদ সংক্রান্ত সমস্যা","translationIssue","thumb-down"],["নমুনা / কোড সংক্রান্ত সমস্যা","samplesCodeIssue","thumb-down"],["অন্যান্য","otherDown","thumb-down"]],["2025-07-29 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)"]]