tags:

views:

76

answers:

1

Does switching activities in Android start a fresh JVM? It seems like each activity is meant to run as its own "main" method. If I have a singleton (via Guice, not an actual singleton in this case) should I expect to be re-creating it every time I switch activities?

+2  A: 

What I know about Android and programming in general is entirely hacked-together and not formally taught so perhaps other people will need to correct me, but that said, I don't think Android starts new virtual machines all the time. I've read that it uses the Dalvik VM rather than the standard JVM. I couldn't tell you what exactly that entails though. However switching activities is meant to be a lightweight operation as an application can easily have several of them. If it had to recreate the Dalvik VM each time, that wouldn't seem to make sense, particularly as Activities can easily pass information between each other, call each other's methods, etc. You can even have one activity start entirely separate applications/tasks while still allowing some communication between them, so again I don't think it would have to start another Dalvik VM in that situation either.

Judging from your other question about why we'd want to have multiple activities I'm assuming you're new to Android; have you had a good read through its documentation, such as the fundamentals, particularly the activity lifecycle? Sometimes the information is a bit sparse, and often they assume the reader understands Java already, but that shouldn't be an issue for you.

Steve H
Generally speaking, each application gets a single VM in a single process. So, starting one of your own activities usually will not create a new VM. Starting somebody else's activity (e.g., opening the Browser on a URL) may well start a new process and give that process its own VM, depending on whether that app already was running in a process.
CommonsWare