gpt4 book ai didi

android - 使用 AndroidAnnotations 库时何时注入(inject) View ?

转载 作者:行者123 更新时间:2023-11-29 16:30:50 24 4
gpt4 key购买 nike

我想知道什么时候将 @ViewById 注释的 View 注入(inject)到 AndroidAnnotations 中。基本上,我想知道在 onResume 期间访问这些 View 之一是否安全?我假设它们是在 onCreate 期间注入(inject)的,但需要确认。

谢谢。

最佳答案

准确了解注入(inject)发生时间的最简单方法是检查 AndroidAnnotations 生成的代码。对于您的示例,我制作了一个简单的 Activity 和 Fragment,如下所示:

@EActivity(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {

@ViewById(R.id.textView)
TextView textView;

@AfterViews
public void activityTestMethod() {

}

}
@EFragment(R.layout.fragment_main)
public class MainFragment extends Fragment {

@ViewById(R.id.imageView)
ImageView imageView;

@AfterViews
public void fragmentTestMethod() {

}

}

然后运行./gradlew app:assembleDebug强制AndroidAnnotations生成相应的类MainActivity_MainFragment_。先来看MainActivity_(无关代码省略):

public final class MainActivity_
extends MainActivity
implements HasViews, OnViewChangedListener
{
@Override
public void onCreate(Bundle savedInstanceState) {
OnViewChangedNotifier previousNotifier = OnViewChangedNotifier.replaceNotifier(onViewChangedNotifier_);
init_(savedInstanceState);
super.onCreate(savedInstanceState);
OnViewChangedNotifier.replaceNotifier(previousNotifier);
setContentView(R.layout.activity_main);
}

private void init_(Bundle savedInstanceState) {
OnViewChangedNotifier.registerOnViewChangedListener(this);
}

@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
onViewChangedNotifier_.notifyViewChanged(this);
}

@Override
public void onViewChanged(HasViews hasViews) {
this.textView = hasViews.internalFindViewById(R.id.textView);
activityTestMethod();
}

}

导致我们的 View 被绑定(bind)和我们的 @AfterViews 方法被调用的事件序列如下:

  • onCreate 中,MainActivity_ 实例被注册为 OnViewChangedNotifier
  • onCreate 调用 setContentView
  • setContentView 调用 notifyViewChanged,这会触发对 onViewChanged 的(同步)调用。
  • onViewChanged 绑定(bind)所有用 @ViewById 注释的字段,然后调用所有用 @AfterViews 注释的方法。

因此,@ViewById-注解的 View 在onCreate被调用后被绑定(bind)并可用,而@AfterViews-注解的方法将在 onCreate 结束时和任何其他 Activity 生命周期方法之前执行。

MainFragment_ 的情况类似:

public final class MainFragment_
extends com.stkent.aatest.MainFragment
implements HasViews, OnViewChangedListener
{
@Override
public void onCreate(Bundle savedInstanceState) {
OnViewChangedNotifier previousNotifier = OnViewChangedNotifier.replaceNotifier(onViewChangedNotifier_);
init_(savedInstanceState);
super.onCreate(savedInstanceState);
OnViewChangedNotifier.replaceNotifier(previousNotifier);
}

private void init_(Bundle savedInstanceState) {
OnViewChangedNotifier.registerOnViewChangedListener(this);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
onViewChangedNotifier_.notifyViewChanged(this);
}

@Override
public void onViewChanged(HasViews hasViews) {
this.imageView = hasViews.internalFindViewById(R.id.imageView);
fragmentTestMethod();
}
}

导致我们的 View 被绑定(bind)和我们的 @AfterViews 方法被调用的事件序列如下:

  • onCreate 中,MainFragment_ 实例被注册为 OnViewChangedNotifier
  • onViewCreated 调用 notifyViewChanged,这会触发对 onViewChanged 的(同步)调用。
  • onViewChanged 绑定(bind)所有用 @ViewById 注释的字段,然后调用所有用 @AfterViews 注释的方法。

因此,@ViewById-注解的 View 在onViewCreated被调用后被绑定(bind)并可用,而@AfterViews-注解的方法将在 onViewCreated 结束时和任何其他 Fragment 生命周期方法之前执行。

在我们的两个示例中,所有 View 绑定(bind)都是在比 onResume 早得多的生命周期方法中执行的,因此您可以安全地在那里访问它们:)

关于android - 使用 AndroidAnnotations 库时何时注入(inject) View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55753957/

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