I have one Activity with a ViewGroup that I want to use to replace by a couple of fragments depending of the user flow (so I'll either show one fragment or the other).
(...)
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="276dp"
android:layout_gravity="center_horizontal|bottom">
</FrameLayout>
(...)
To set the fragment, I'm using the following code:
private void setBottomFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
If I instantiate the fragment myself with new, it works:
setBottomFragment(new InformationFragment());
However, I don't want to instantiate it myself. I want my fragment instances to be managed by my Dependency Management framework (I'm using Roboguice)
So I'd like to do something like this:
@ContentView(R.layout.activity_scan)
public class ScanActivity extends RoboFragmentActivity {
@InjectFragment(R.id.information_fragment)
private InformationFragment informationFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setBottomFragment(informationFragment);
}
}
By doing this, I'll be able to use dependency injection also on my Fragments, instead of having to find the components myself.
public class InformationFragment extends RoboFragment {
// I'll have an event handler for this button and I don't want to be finding it myself if I can have it injected instead.
@InjectView(R.id.resultButton)
private Button resultButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_information, container, false);
}
}
So basically my question is: how can I instantiate fragments programaticly while using a dependency injection framework like Roboguice?
All I can find in Roboguice documentation are examples of DI with fragments that are initially rendered with the view and not dynamically added afterwards, like I'm trying to do. That leads me to believe I'm missing something either in the components' lifecycle or the way Roboguice handles the DI.
Any ideas to fix this or pointers in the right direction would be very helpful.
Found the answer myself and since it might be helpful to someone else, I'll post it here.
It's a lot simpler than I expected (and a bit obvious too). Because the fragment is not part of the view when the activity is first loaded, we cannot use @InjectFragment. @Inject should be used instead, so the fragment is treated like any other non-view dependency.
That means my activity should look like this:
@ContentView(R.layout.activity_scan)
public class ScanActivity extends RoboFragmentActivity {
@Inject // This is all that needs to change
private InformationFragment informationFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setBottomFragment(informationFragment);
}
}
That simple...