I'm working on a Android app which uses the Roboguice dependency injection framework.
So most of the time we extend RoboActivity, RoboListActivity and similar.
Now I would like to introduce some sort of global error handling which will show some alert or a error activity in case the application crashes.
I have done this before by implementing a base activity like this:
public class BaseActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new GeneralError(this));
}
where I define the default exception handler and all other activities then derived from this one.
Now I'm wondering how this is achieved with Roboguice?
Here is some rough psuedo code that should get you started. It uses the roboguice events to make some of these cross cutting concerns a little easier.
public class GlobalErrorHandler {
// injects the current activity here
#Inject Context context;
public void onCreate(#Observes OnCreateEvent e) {
// Wires up the error handling
Thread.setDefaultUncaughtExceptionHandler(new GeneralError(context));
}
}
public class MySpecificActivity {
// required in every activity that needs error handling
#Inject GlobalErrorHandler errorHandler;
}
Related
I just started with DI / Dagger 2. I have a huge project. To try dagger 2, I started with injecting my singleton class 'MyPreferences'. This class handles all app actions from and to the SharedPreferences.
Auto-injecting the SharedPreferences in the MyPreferences went perfectly.
However, I am using this singleton in about 50 classes, I used to do this:
MyPreferences.getInstance(context).getXXXSetting();
I have changed this to dagger2 injection in a few classes, and it works fine, but I find myself copying these lines all the time:
#Inject
protected MyPreferences myPreferences;
protected void initInjection(Context context) {
((RootApplicationDi) context.getApplicationContext()).getComponent().injectTo(this);
}
// + call initInjection # onCreate / constructor
For such a simple call I need all these lines in about 35-40 (super) classes. Am I missing something? Is this really the way to go?
My previous answer was for Dagger 1 and thus incorrect. Here is example solution for Dagger 2:
In your application class:
private MyDagger2Component mDependencyInjector;
#Override
public void onCreate() {
super.onCreate();
mDependencyInjector = DaggerMyDagger2Component.builder()...build();
}
public MyDagger2Component getDependencyInjector() {
return mDependencyInjector;
}
In your base Activity class which your activities extend:
protected MyDaggerComponent getDependencyInjector() {
return ((MyApplication) getApplication()).getDependencyInjector();
}
And in your activity you can now have just one line for the injection:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getDependencyInjector().inject(this);
...
}
I am studying a Dagger 2 from many sources such as this one: http://fernandocejas.com/2015/04/11/tasting-dagger-2-on-android/
but I still haven't found an answer to my question.
I work on quite complex application with tens of fragments and several activities in which I want to use DI (dagger 2). For all of those fragments and activities I have one BaseActivity and one BaseFragment. However, as far as I read and tried, in order to use #Inject in my let's say MainActivity, I have to specify it in Component interface and also invoke getApplicationComponent().inject(this) in onCreate method. When I do this for BaseActivity only, #Inject annotated fields in MainActivity is never injected. And what is even worse, I do not find out about that until that specific part of code is executed and NPE is thrown.
So far it is a deal breaker for me, because this can be source of many crashes. I would need to specify tens of fragments and activities in Component interface and not forget to call inject in each onCreate method.
I would be very glad to hear any solution to this since I would really like to use DI..
code example:
#Singleton
#Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
void inject(BaseActivity baseActivity);
Analytics analytics();
}
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getApplicationComponent().inject(this);
}
}
public class MainActivity extends BaseActivity {
#Inject
Analytics analytics;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
analytics.log("event1"); // THROWS NPE!
}
}
You can not inject properties in your subclass by injecting the super (since dagger2 works at compile time and there is no way to dynamically check subclasses for annotated properties.)
You can move analytics up to the super, then it will be injected there. To inject annotated fields in your subclass you will have to call the injection there again.
You can make an abstract method in your baseclass e.g. inject(App app)where you just handle the injection. That way you can't 'miss' it.
As stated in the official documentation:
While a members-injection method for a type will accept instances of its subtypes, only Inject-annotated members of the parameter type and its supertypes will be injected; members of subtypes will not.
move the
#Inject
Analytics analytics;
to your BaseActivity class, the Analytics object is initialized in the superclass and is inherited by sub-classes automatically, therefor u wouldn't get null any more.
public class MainActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
analytics.log("event1");
} }
I have the following test code. I am trying to test whether a list is being populated from SQLite database.
public class ViewIssuesActivityTest extends BaseActivityTest<ViewIssuesActivity>{
private List<Issue> issues;
public ViewIssuesActivityTest() {
super(ViewIssuesActivity.class);
}
public void setUp() throws Exception {
super.setUp();
issues = new ArrayList<Issue>();
issues.add(new Issue("Trial","Desc","Location","ImagePath"));
IssueRepository issueRepository = mock(IssueRepository.class);
doAnswer(new Answer<Object>() {
#Override
public List<Issue> answer(InvocationOnMock invocation) throws Throwable {
return issues;
}
}).when(issueRepository).getIssues();
activity.setIssueRepository(issueRepository);
}
public void testNumberOfIssuesRecorded() {
ListView listView = (ListView) activity.findViewById(android.R.id.list);
assertEquals(1, listView.getCount());
}
}
My BaseActivityTest code is:
public class BaseActivityTest<T extends Activity> extends ActivityInstrumentationTestCase2<T> {
protected T activity;
public BaseActivityTest(Class<T> activityClass) {
super(activityClass);
}
#Override
protected void setUp() throws Exception {
activity = getActivity();
}
}
My ViewIssuesActivity is as follows:
public class ViewIssuesActivity extends ListActivity{
private IssueRepository issueRepository;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.change.kranti.R.layout.issues);
issueRepository = new IssueRepository(getApplicationContext());
List<Issue> issues = new ArrayList<Issue>();
issues = issueRepository.getIssues();
ArrayAdapter<Issue> adapter = new ArrayAdapter<Issue>(this,
android.R.layout.simple_list_item_1, issues);
setListAdapter(adapter);
}
}
The issue is I get error: expected<1> got <0>
I think the issue is the onCreate method is getting called before the issueRepository is created.
I want to mock the IssueRepository and test my list getting populated.
What is the issue with my code or is there a better way to test this functionality.
Any help will be appreciated.
You are right. The issue is that the onCreate method gets called before the mock issueRepository gets injected. When you call getActivity in the context of an ActivityInstrumentationTestCase2, all the usual life cycle methods get called (i.e., onCreate, onStart, and onResume). When your test code calls setIssueRepository, it's already too late, the issues have been fetched from the repository.
The best solution I know in this kind of case is to use dependency injection, whether by hand or using a DI framework.
It's not clear what you're trying to do.
You can't run two tests simultaneously against the same class. The ActivityInstrumentationTestCase2 class is essentially a controller and wrapper for the Activity under test, but it can only control a single Activity, and you can't control the same Activity with multiple instances of ActivityInstrumentationTestCase2.
To set up an app for test-driven development, try to use as many POJOs (Plain Old Java Objects) as you can. Limit the Activity testing to stuff such as sending Intents or integration between POJOs that you can mock.
ActivityInstrumentationTestCase2 is a JUnit TestCase subclass, but it has limitations.
That's probably part one of my question.
Basically I'm struggling with the actual injection for version 1.1.2. I've read the couple of pages on the site, and I feel I'm missing something.
Basically I've done the RoboApplication extension. I've overridden the addApplicationModules method. I've even made a module.
My module looks like this:
public class DataRepository extends AbstractAndroidModule
{
#Override
protected void configure() {
/*
* This tells Guice that whenever it sees a dependency on a TransactionLog,
* it should satisfy the dependency using a DatabaseTransactionLog.
*/
bind(IDataBaseAdapter.class).to(DataBaseAdapter.class);
}
}
In my adapter I have this:
public class DataBaseAdapter implements IDataBaseAdapter
{
private DataBaseHelper _dbHelper;
private SQLiteDatabase _db;
#Inject
protected static Provider<Context> contextProvider;
public DataBaseAdapter()
{
_dbHelper = new DataBaseHelper(contextProvider.get());
}
}
If I don't do there, where is the opportune place for the chunk of code to reside... where I associate injectors?
Finally... my Application has an injection of it like so:
public class MyApplication extends RoboApplication
{
public MyApplication()
{
super();
}
public MyApplication(Context context)
{
super();
attachBaseContext(context);
}
#Override
protected void addApplicationModules(List<Module> modules)
{
modules.add(new DataRepository());
}
#Inject
private IDataBaseAdapter adapter;
public IDataBaseAdapter getAdapter()
{
return adapter;
}
public void setAdapter(IDataBaseAdapter value)
{
adapter = value;
}
...
}
I'm trying to use the Inject attribute as shown. For example:
#Inject
private IDataProvider provider;
A couple of reasons why I'm lost is that I come from a .NET and Flash/ActionScript background plus I've only used StructureMap instead of Ninject (in the .NET world), which I've heard Guice is designed with some of the ideas of Ninject in mind. Could someone help me figure out this small piece?
I'd really like to focus on using 1.1.2 instead of jumping to 2.x of RoboGuice... especially since it is still in beta, so I hope you all don't mind.
Thanks again,
Kelly
Android is quite different from standalone / hosted java application. You do not have main() , but you have certain activity units, which are managed by android framework (activities, services , broadcast receivers)
DI is a technique which allows you to eliminate booler plate code by wiring together
parts in good object oriented way.
As your unit of work is mostly activity, you shall do wiring / creation of your collaborating objects in onCreate() method , and there are dedicated onResume() and onPause() methods (see actviity lifecycle)
Rule of thumb is, does this thing needs to be restarted every time activity loses it focus? If yes, initialize / destroy it in inResume() / onPause(), otherwise - in onCreate()
And if you like to share objects withing entire application ( running in same JVM ) , it is OK to use singleton pattern in android. So you may just have singleton injector factory , and cosult it from everywhere:
InjectorFactory.getInstance(<context if necessary?>).getInstance(whatever you need);
OK, I've figured out what was needed, but I'm not quite sure why after seeing all the information floating out there.
I basically made this change, and now my test passes.
public class DataBaseAdapter implements IDataBaseAdapter
{
private DataBaseHelper _dbHelper;
private SQLiteDatabase _db;
#Inject
public DataBaseAdapter(Provider<Context> contextProvider)
{
_dbHelper = new DataBaseHelper(contextProvider.get());
}
}
While I like using constructors as the tool for injecting, I wonder why it had to work this way, considering that examples I have seen are some kind of reflection class injection.
Anyway, that's this part. Hopefully someone else will find this useful.
Cheers,
Kelly
Is there any way in android to intercept activity method calls (just the standart ones, like "onStart. onCreate")?
I have a lot of functionality that must be present in every activity in my app, and (since it uses different types of activities (List, Preferences)) the only way to do it is to create my custom extensions for every activity class, which sucks :(
P.S. I use roboguice, but since Dalvik doesn't support code generation at runtime, I guess it doesn't help much.
P.S.S. I thought about using AspectJ, but it's too much of a hassle since it requires a lot of complications (ant's build.xml and all that junk)
The roboguice 1.1.1 release includes some basic event support for components injected into a context. See http://code.google.com/p/roboguice/wiki/Events for more info.
For Example:
#ContextScoped
public class MyObserver {
void handleOnCreate(#Observes OnCreatedEvent e) {
Log.i("MyTag", "onCreated");
}
}
public class MyActivity extends RoboActivity {
#Inject MyObserver observer; // injecting the component here will cause auto-wiring of the handleOnCreate method in the component.
protected void onCreate(Bundle state) {
super.onCreate(state); /* observer.handleOnCreate() will be invoked here */
}
}
You could delegate all the repetitive work to another class that would be embedded in your other activities. This way you limit the repetitive work to creating this object and calling its onCreate, onDestroy methods.
class MyActivityDelegate {
MyActivityDelegate(Activity a) {}
public void onCreate(Bundle savedInstanceState) {}
public void onDestroy() {}
}
class MyActivity extends ListActivity {
MyActivityDelegate commonStuff;
public MyActivity() {
commonStuff = MyActivityDelegate(this);
}
public onCreate(Bundle savedInstanceState) {
commonStuff.onCreate(savedInstanceState);
// ...
}
}
This minimalises the hassle and factorises all common methods and members of your activities. The other way to do it is to subclasse all the API's XXXActivty classes :(
Take a look at http://code.google.com/p/android-method-interceptor/, it uses Java Proxies.