Nếu MyFragment
phụ thuộc vào MyNestedFragment
, và MyNestedFragment
phụ thuộc vào Deps
; nó sau đó MyFragment
cũng phụ thuộc vào Deps
. Tất nhiên, không có cá thể nào của MyNestedFragment
tồn tại khi Activity.onAttachFragment()
được gọi, vì vậy bạn sẽ phải đợi cho đến sau khi bạn đã tăng bố cục trong MyFragment.onCreateView()
trước khi cung cấp MyNestedFragment
với các phụ thuộc của nó.
public class MyActivity {
...
void onAttachFragment(Fragment f){
((MyFragment)f).dependencies(deps);
}
public static class MyFragment extends Fragment {
private Deps deps;
void dependencies(Deps deps) {
this.deps = deps;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
// <fragment> element in fragment_main layout has
// android:tag set to nested_fragment
((MyNestedFragment)getChildFragmentManager()
.findFragmentByTag("nested_fragment"))
.dependencies(this.deps);
return rootView;
}
}
public static class MyNestedFragment extends Fragment {
void dependencies(Deps deps) {
...
}
}
...
}
Nếu tất cả điều này có vẻ lộn xộn một chút, đó là vì Fragments không phải POJO bạn chỉ có thể kết nối một cách tùy ý. Vòng đời của họ phải được quản lý bởi FragmentManagers lồng nhau. Nếu bạn tạo các đoạn của bạn theo chương trình thay vì sử dụng phần tử >, bạn sẽ có quyền kiểm soát nhiều hơn đối với vòng đời của mình với chi phí phức tạp hơn.
Nếu bạn muốn đối xử với Android như một container IoC, sau đó RoboGuice có thể là những gì bạn đang tìm kiếm:
public class MyActivity extends roboguice.activity.RoboFragmentActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
// This only needs to be called once for the whole app, so it could
// be in the onCreate() method of a custom Application subclass
RoboGuice.setUseAnnotationDatabases(false);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static class MyNestedFragment extends Fragment {
@Inject
private Deps deps;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// this isn't necessary if you extend RoboFragment
roboguice.RoboGuice.getInjector(activity).injectMembers(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//This would not even be possible in the previous example
// because onCreateView() is called before dependencies()
// can be called.
deps.method();
View rootView = inflater.inflate(R.layout.fragment_nested, container, false);
return rootView;
}
}
}
@Singleton
public class Deps {
public void method() {
System.out.println("Deps.method()");
}
}
Sử dụng dagger2? Nó được thiết kế để quản lý những thứ như thế này –
Mimmo Grottoli, tôi biết về dagger2. Nhưng nó chỉ là một thư viện để loại bỏ mã boilerplate của tiêm phụ thuộc. Luôn luôn có cách để tiêm phụ thuộc bởi constructor hoặc phương pháp đặc biệt. – wilddev
Một nhà xây dựng cho các mảnh vỡ hoặc hoạt động đưa vào một số phụ thuộc?Tất nhiên bạn có thể thử, nhưng cuối cùng bạn sẽ thấy rằng dao găm hoặc dao găm 2 là những điều tốt nhất mà bạn có thể tự phát triển (ít nhất, điều này đúng với tôi) –