Exactly when can I expect an app to be paused/destroyed?

Put your problem here if it does not fit any of the other categories.

Exactly when can I expect an app to be paused/destroyed?

Postby Fome » Thu Nov 19, 2009 5:04 pm

Hi,

I'm new to android programming and have read about how the life-cycle works, but I still have some questions that I can't find answers to.

I get that the system is responsible for the life-cycles of the activities and that it can "at any moment" pause your program, but what does "at any moment" mean? Can it shut down in the middle of a method?

Let's say I have a method that has a for-loop which is supposed to do something a hundred times. E.G.:

int i;
for (i=1; i<100; i++) {
//do something here
}

Do I have to expect that the activity can shut down in the middle of the loop (say after 50 iterations)? How are you supposed to resume the activity if this is the case? If you call the method again it will start with i=1 and not 50 as it's supposed to.

If on the other hand the system waits for the method to finnish, what stops me from doing while(true) { } and making it impossible to destroy the app?

Thanks for any answer! :)
Fome
Freshman
Freshman
 
Posts: 6
Joined: Wed Nov 18, 2009 10:59 pm

Top

Re: Exactly when can I expect an app to be paused/destroyed?

Postby divestoclimb » Thu Nov 19, 2009 7:05 pm

I'm not 100% positive, but I think the phone simply broadcasts a message that's received by the main thread of your activity. If you're doing lots of heavy work in your main thread, though, your users are first going to see the Application Not Responding dialog prompting them to force close, and that will stop your activity in the middle of what it's doing and it won't save state. I suspect it will also make them really pissed off if they miss a phone call because your app refused to close in time :)

If you have a lot of work to do, do it in a thread. If it's important to save the progress of the thread, have the thread dispatch periodic messages to your activity so it can store the state of the thread. Then, in the activity, add a call to interrupt() the thread in onPause() or onStop().

A simple example from one of my apps (though it doesn't interrupt on pause yet): http://code.google.com/p/d2cchecklist/s ... store.java
divestoclimb - Android SCUBA app developer
Android Community | Gas Mixer, ScubaLib, Scuba Tanks | d2c Checklist
All code is open source under Apache license.
divestoclimb
Developer
Developer
 
Posts: 33
Joined: Mon May 11, 2009 7:46 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 10 guests