Skip to content
tylertreat edited this page Dec 31, 2012 · 3 revisions

ContextFactory is used to configure Infinitum and to retrieve an instance of InfinitumContext. Before a context can be retrieved, ContextFactory's configure method must be invoked by passing in a Context and a resource ID for infinitum.cfg.xml. If a call is made to get an InfinitumContext before configure has been invoked, an InfinitumConfigurationException will be thrown.

ContextFactory is not instantiated directly but rather retrieved by invoking its static method newInstance.

Acquiring an InfinitumContext

The following code snippet illustrates how Infinitum is configured and how an InfinitumContext singleton is retrieved.

ContextFactory.newInstance().configure(this, R.raw.infinitum);
InfinitumContext ctx = ContextFactory.newInstance().getContext();

ContextFactory's configure method takes a Context as its argument. Because configure returns an InfinitumContext instance, the above code can be made more concise:

InfinitumContext ctx = ContextFactory.newInstance().configure(this, R.raw.infinitum);

The ID for the XML configuration does not need to be specified if using the standard naming convention, infinitum.cfg.xml:

InfinitumContext ctx = ContextFactory.newInstance().configure(this);

Note that manually calling configure and interacting with the ContextFactory is unnecessary if your Activity or Fragment extends one of Infinitum's Activity or Fragment classes because these classes handle framework initialization and expose an InfinitumContext.

public class MyActivity extends InfinitumActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        InfinitumContext ctx = getInfinitumContext();
        // ...
    }

}

See the InfinitumActivity or InfinitumFragment pages for more information.

ContextFactory also provides a way to access module contexts using the overloaded getContext method. This gives you access to module-specific APIs like the ORM's Session interface (this can also be done through the parent InfinitumContext).

InfinitumOrmContext orm = ContextFactory.getContext(InfinitumOrmContext.class);

The above code is effectively the same thing as the following:

InfinitumContext context = ContextFactory.getContext();
InfinitumOrmContext orm = context.getChildContext(InfinitumOrmContext.class);

Clone this wiki locally