Android Fragment and Dependency Injection

as the title says, I'm trying to figure out which one is the best way to insert a dependency in a fragment. I want to be independent of external frameworks like RoboGuice etc.

Now, in the simplest way, I have an interface that abstracts some logic, and from Activity I want to implement an implementation of this interface. I know that I need to provide a default constructor for my fragment, because at some point the system may need to recreate the fragment, and the usual way to create a new instance of the fragment is to provide a static method that handles the creation like this:

public static Fragment newInstance() { final Bundle bundle = new Bundle(); ... final Fragment fragment = new MyFragment(); fragment.setArguments(bundle); return fragment; } 

How to convey your dependence on a fragment? Should I implement a Parcelable or Serializable interface and then pack it in a bundle? Is there any other way to achieve the result?

Thanks!

+7
android dependency-injection dependencies code-injection fragment
source share
3 answers

A simple solution is to declare an interface that declares the dependencies needed for the fragment. Then let the Fragment context implement this interface and interrogate the dependencies when necessary from the context.

Contract:

 public interface MyDependencies { MyDep getDep(); } 

Activity:

 public MyActivity extends Activity implements MyDependencies { @Override public MyDep getDep(){ return createMyDependencyOrGetItFromASingletonOrGetItFromApplication() } } 

Fragment:

 public void onActivityCreated(Bundle b){ super.onActivityCreated(b) if (getActivity() instanceOf MyDependencies) { MyDep dep = ((MyDependencies) getActivity).getDep(); } else { throw new RuntimeException("Context does not support the Fragment, implement MyDependencies") } } 

Thus, in fact, there is no unnecessary connection with the Activity, because the contract is defined by the interface.

+7
source share

Why not grab the addiction to your business?

 public void onActivityCreated( Bundle b){ super.onActivityCreated(b) DependencyClass c = ((MyActivity)getActivity()).getDependency(); } 
0
source share

If you cannot pass the dependency through a constructor (if you need a default constructor), and you do not want to use an injection lib dependency such as Dagger or RoboGuice, another classic way is to set up the dependency in the customizer.

 Fragment MyFragment { Depend mDepend; .... public void setDepend(Depend depend) { mDepend = depend; } } 

Then, in your activity, you can add a dependency to the onCreate method.

something like this in your activity:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map); MapFragment wrapperFragment = new WrapperFragment(); if (savedInstanceState == null) { getFragmentManager().beginTransaction() .add(R.id.map_container, wrapperFragment).commit(); // find the fragment // call the setter } } 
0
source share

All Articles