gpt4 book ai didi

java - Android:支持向后兼容

转载 作者:行者123 更新时间:2023-12-02 13:11:43 25 4
gpt4 key购买 nike

我很困惑,当我们在 android studio 中构建项目时,我们被要求定义我们想要支持的最新版本,如果我们不包含支持向后兼容性的支持库怎么办?那么我得到的方法将是最新版本,旧版本将无法运行我们的代码,对吗?我也无法理解 documentation 中的这些行

Note: FragmentActivity is a special activity provided in the Support Library to handle fragments on system versions older than API level 11. If the lowest system version you support is API level 11 or higher, then you can use a regular Activity

但如果在这种情况下我扩展 Activity 而不是 FragmentActivity

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().findFragmentById(R.id.frag_map);
}

getSupportFragmentManager 未解决,尽管我正在构建高于 API 11 的项目,但为什么他们说你应该使用 Activity?如果我使用 getSupportFragment 则未解决。

后来在文档中他们说:

If you're using the v7 appcompat library, your activity should instead extend AppCompatActivity, which is a subclass of FragmentActivity. For more information, read Adding the App Bar).

v7 不支持 API 11 之前的版本吗?

最佳答案

更新:

根据 24.2.0 及更高版本中兼容库支持版本控制的变化,v4 和 v7 的最低支持版本已变得相同,即 API 级别 9。以下是文档现在所说的内容:

Some of the Support Library packages have package names to indicate the minimum level of the API they support, using a v# notation, such as the support-v4 package. Starting with Support Library version 24.2.0 (released in August 2016), the minimum supported API level has changed to Android 2.3 (API level 9) for all support library packages. For this reason, when working with any recent release of the support library, you should not assume that the the v# package notation indicates a minimum API support level. This change in recent releases also means that library packages with the v4 and v7 are essentially equivalent in the minimum level of API they support. For example, the support-v4 and the support-v7 package both support a minimum API level of 9, for releases of the Support Library from 24.2.0 and higher.

主要区别在于各自引入的具体功能之间。两者都有不同的功能集。例如,AppCompatActivity位于 v7 而 FragmentActivity在于v4。在这种情况下,AppCompatActivity应该是首选,但其他功能不会有很大的交叉。

阅读此处:https://developer.android.com/topic/libraries/support-library/index.html

免责声明:在选择支持最低版本时,您必须仔细选择交叉版本。因此,强烈建议您引用文档,因为 Android API 更新得非常快,并且此类答案已经过时。

<小时/>

原始答案:

你把自己搞糊涂了。这真的很简单。据我了解:

  • 支持类需要支持组件,即 Activity、Fragment 等。因此,支持 Fragment 管理器需要 FragmentActivityAppCompatActivity 。因此,在使用 getSupportFragmentManager 时不包括支持库势必会给出编译时错误。
  • 由于 Fragment 是在 API 级别 11 中引入的,因此 FragmentActivity 将用作 API 级别 < 11 上的支持组件,这样您就不必编写类似 if(Build.VERSION_CODES < HONEYCOMB){} 的代码。每个 Android 版本中发布的标准和关键 Android 功能。但 FragmentActivity 在 v4 兼容库中。如果您不打算支持该支持,请使用 v7 版本 AppCompatActivity
  • 您的常规 Activity 应该是目标 Android API 中的 Activity ( android:targetSdkVersion )。

另请参阅 Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which?

关于java - Android:支持向后兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43934515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com