3

Background

I have to use Roboguice for an app, which handles a lot of injections on many classes.

The app also has a splash screen class which extends from RoboSplashActivity .

The problem

As the app got more and more complex, more time was spent on the splash screen activity, and it even shows its content layout after a while , meaning the user sees a blank screen about 1-2 seconds before there is even a splash image.

What I've tried

At the beginning I thought it was because the splash image was too have (since it had multiple layers of images) so I tried setting a simple color. Turns out it's not the reason.

Then I thought that it might be that the app takes a lot of space, so I created a totally new project with the same size, and it worked fine. so a large app isn't the reason for slow start up.

Then I thought that it's the RoboSplashActivity's fault, so I've replaced it with a new activity (extending Activity instead ) that only shows a solid color background. the background showed after a while , almost the same time as using RoboSplashActivity . Still not the reason for the blank screen.

Now what I think is that it's Roboguice's fault, and that I should somehow delay its initialization to the time that something is shown on the screen, so that at least the user will see something while it's loading.

The question

Is it possible to optimize Roboguice to have the minimal start time ?

Maybe delay its initialization that is done on other files ?

android developer
  • 114,585
  • 152
  • 739
  • 1,270

1 Answers1

1

I'm using Guice and I had the same problem kind of problem. I'm sure the solution to your problem is very similar.

The thing that is taking a lot of time is to create the dependency graph as defined in your modules. There's a lot of reflection going on and it will take some time to analyze all your bindings. You need to to move your dependency injection setup to a separate thread/task that you kick-off in your SplashActivity.

Since you're using RoboGuice there's already an activity designed specifically for this. Have a look at the RoboSplashActivity.

UPDATE: I can't believe that I overlooked the part where you wrote that you already extend RoboSplashActivity. Sorry about that. As I wrote in the comment:

Do you subclass Application and do any dependency injection there?

As a side-note when it comes to startup time: You may want to look into the Stage setting for controlling the way the dependency injection is set up. For Guice there's three modes, each with different startup times. It seems like RoboGuice has the same and it defaults to Stage.PRODUCTION.

britzl
  • 10,132
  • 7
  • 41
  • 38
  • i've already written that i use the RoboSplashActivity. however, maybe i'm doing something wrong? how can i put the dependency injection setup on a separate thread? – android developer May 28 '13 at 14:34
  • Ah, sorry for not reading your question properly. The thing with the RoboSplashActivity is that it takes care of all that for you. If you look at the link I posted you can see the sourecode yourself. It creates a thread and all. Do you happen to have subclassed Application and if so, do you do you use a RoboGuice injector there? – britzl May 28 '13 at 17:29
  • yes you are correct. putting the code of initialization in the background thread of the splash screen works and makes the bootup much faster , but i fear that it might cause some problems, as i have services and broadcastReceivers that can work without the need of the splash screen. what should i do ? – android developer May 29 '13 at 07:03
  • Ah well, with a service or broadcast receiver you will probably have to accept that they will take longer to start up. Do they need injection of a lot of stuff btw? – britzl May 29 '13 at 10:57
  • it's quite problematic. you are correct on that too. i wish we didn't use roboguice at all, but i do agree that it has its advantages. on those components, i think it's critical to have less injections as possible, but because we use injections for almost everything, i hope it won't be a problem. is it possible to avoid the initializations of roboguice for such components, and tell it to auto-inject by demand(only when it needs to, and not before) ? – android developer May 29 '13 at 11:10
  • Well yes. Do not extend RoboService and instead do the injection manually and when it's needed. Take a look at what the RoboService is doing in it's onCreate() method: https://github.com/roboguice/roboguice/blob/master/roboguice/src/main/java/roboguice/service/RoboService.java#L58 You can do this manually when needed – britzl May 29 '13 at 11:51
  • Remember that your application might be restored by the system (if it was killed to reclaim resources for example). If that's the case, you might not pass by your splash screen activity. You need to make sure your app is working even if the splash screen is not shown – nicopico Jun 12 '13 at 10:18