Skip to content
tylertreat edited this page Dec 30, 2012 · 1 revision

Application-wide context information, which is read from the infinitum.cfg.xml file, is stored in a container called InfinitumContext. Since the information stored in InfinitumContext is initialized from the configuration file, this object should not be instantiated directly. Rather, it should be obtained, as a singleton, through the ContextFactory.

Configuration

InfinitumContext is also configured in ContextFactory in addition to being obtained from it. Invoking the configure method in ContextFactory is critical and must be done before any attempts are made to access the context. The following code snippet illustrates this notion:

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

The above example will throw an InfinitumConfigurationException because configure has not been called on the ContextFactory. The corrected code is as follows:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ContextFactory.newInstance().configure(this);
    InfinitumContext ctx = ContextFactory.newInstance().getContext();
}

ContextFactory's configure method takes a Context as its argument. This method will implicitly configure Infinitum using the infinitum.cfg.xml file placed in the res/raw directory. Because configure returns an InfinitumContext instance, the above code can be made more concise:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    InfinitumContext ctx = ContextFactory.newInstance().configure(this);
}

The XML configuration file used to configure Infinitum can be specified by using the overloaded configure method illustrated below:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    InfinitumContext ctx = ContextFactory.newInstance().configure(this, R.raw.myconfig);
}

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.

Using InfinitumContext

InfinitumContext can be used to evaluate application-configuration settings at runtime. It also exposes a service-locator API for retrieving beans.

MyService service = context.getBean("myService", MyService.class);

More important, if you're using additional Infinitum modules, such as the ORM, InfinitumContext allows you to retrieve their configurations. This gives you access to module-specific APIs like the ORM's Session interface (this can also be done through the ContextFactory.

InfinitumOrmContext orm = context.getChildContext(InfinitumOrmContext.class);
Session session = orm.getSession(SessionType.SQLITE);

Clone this wiki locally