Hi, I'm struggling with documentation in conjunction with my huge lack of experience in java, in general, and java threading specifically. I've got a little App that works okay, but currently breaks many of the android life-cycle guidelines, which is fine for my messing around and learning, but I'm trying to get to stage two, which is make it fit the models presented. I've got a user interface (an Activity) and a Service which contains a broadcast receiver. The user interface explicity starts the Service (which is declared in my manifest). The broadcast reciever is monitoring battery changes. I need to get the battery change information to the activity. No big deal for my toy version held carefully and no launching home or other apps. I'm concerned about various life-cycle scenarios regards things like the activity being stopped and re-launched (screen rotation) and the like. So, I'm going to summarize how I think the states look and hope that somebody on this list can confirm my understanding or point me in the right direction....
call the GUI Activity class "A", the Service "B" started from A with "startService(new Intent(A.this, B.class));". I'm going to throw a new thread in here to make sure I really have the full picture, call the thread runnable "C" and it is started from Service B. A has at least one button with a listener method registered. My life-cycle understanding, PLEASE CONFIRM or CORRECT: WHEN THE PROCESS HAS THE FOCUS AND THE SCREEN: A, B methods are running on the main thread (call it "1"). If A & B are in the same package and variables are public, they can read/write each others variables. The execution model suggests that when A returns from OnCreate, OnResume, OnStart, no other user level code (methods) run until an event occurs (button press or life-cycle). And for Service B, I have the same assumption, after return from OnCreate, no user level code executes until either the broadcast receiver or some life-cycle event occurs. An implication of this, to me, is that execution must return from the life-cycle methods or other life-cycle methods in the class and/or other registered methods (listeners) will never be invoked??? Now for the runnable "C" launched from the Service, we still are sharing memory (threads) so public variables in A & B are accessible from C in thread ("2"). If C returns from its runnable thread 2 goes away! Now, is the part i'm more concerned about, for one reason or another, the system decides TO get rid of Activity A, OnDestroy is invoked, does its thing and returns, my understanding of the state is: Service B is still executing on thread (1), and runnable "C" is still alive on thread (2). However, all references to public variables, including static class variables, of Activity A are now-undefined (perhaps null?). So if either B or C make references to A's variables, bad things are probably going to happen. However public data access between B & C still works? Am I totally off-base? I.e. is it much simpler; such that since A started B and they are on the same thread, they both are destroyed when the system decides to destroy A??? FINALLY, the system recreates activity A (perhaps it was killed by a screen rotation). I've questions here: A runs on some thread, but as far as I can tell, there is no guarantee that this thread has anything to do with thread (1) which has service B and definitely bears no relation to thread (2) with runnable C. The startServcie call in A does not create a new instance of Service class B because the system knows that B is active on thread (1); i.e. B.onCreate is not invoked, but its onStartCommand will be??? And, because Activity A is a new instance of A, any references in Service B or runnable C to public members of A will still cause bad things to happen. I really hope somebody can wade through this, maybe I should have done ASCII are instead of verbal descriptions? thanks in advance for any replies. -- You received this message because you are subscribed to the Google Groups "Android Beginners" group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to android-beginners+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en