win*_*ton 64 android android-fragments android-activity android-support-library fragmentmanager
My Fragment onAttach(context)在启动时不会调用方法AppCompatActivity.
以XML格式创建片段:
<fragment
android:id="@+id/toolbar"
class="package.MainToolbarFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="@layout/fragment_main_toolbar" />
Run Code Online (Sandbox Code Playgroud)
但如果我扩展它support.v4.Fragment,onAttach(context)请致电!
可能是什么问题呢?
当然,我可以扩展所有片段 v4.Fragment,但我不想要它.这是不好的做法吗?同时项目min sdk 14.
soc*_*qwe 130
它未被调用,因为此方法已在API 23中添加.如果您在具有API 23(marshmallow)的设备上运行应用程序,onAttach(Context)则将调用该方法.在所有以前的Android版本onAttach(Activity)将被调用.
http://developer.android.com/reference/android/app/Fragment.html#onAttach(android.app.Activity)
支持库片段与平台无关.因此它适用于所有API版本.
Joh*_*ohn 43
虽然Google希望我们停止使用已弃用的API
@Override
public void onAttach(Context context) {
super.onAttach(context);
...
Run Code Online (Sandbox Code Playgroud)
它是如此新颖,以至于没有被广泛称呼.你还需要实现
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
...
Run Code Online (Sandbox Code Playgroud)
对我来说,他们是相同的,但我喜欢KISS,并介绍另一个支持库往往将我的apk加倍到大约1000kb.我昨天才更新了我的SDK.
究其原因,类型是不能互换位置,因为他们在很多情况下,是拿一个方法Activity时仍然会叫Activity,因为他们都是公开可见的,并且提供Activity比更专业的(作为一个子类),Context所以会取得优先权.
| 归档时间: |
|
| 查看次数: |
21776 次 |
| 最近记录: |