对于普通(非嵌套 fragment )我使用以下方法
1) create dependencies(...) 方法来设置 fragment 的依赖
class MyFragment extends MyFragment {
void dependencies(Deps deps);
}
2) 在 MyFragment 父 Activity 的 onAttachFragment() 方法中,我只是为 fragment 提供依赖项
class MyActivity{
void onAttachFragment(Fragment f){
((MyFragment)f).dependencies(deps);
}
}
对于嵌套 fragment ,不再调用 onAttachFragment fragment 。 为fragment提供依赖只是为了给nested fragment提供依赖,看起来很麻烦。那么我该如何为它提供依赖关系呢?
最佳答案
只需脱离上下文即可,这将是一项 Activity 。为 Activity 的依赖项创建一个 getter。 fragment 可以访问父 Activity ,无论是否嵌套。转换上下文,然后调用 getter 以获取嵌套 Activity 中的依赖项。
关于android - 如何将依赖项注入(inject)嵌套的android fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33058983/