Process death and onDestroy() (NOT the usual question)

466 views
Skip to first unread message

Tamás Kovács

unread,
Apr 23, 2012, 7:04:02 PM4/23/12
to android-platform
Hello,

I've thoroughly read the official docs but I've two questions.

Of course, I know that onDestroy() is not guaranteed to be called when
the activity (or even the process) is killed. My question is exactly
the opposite direction: if an onDestroy() call it detected, will the
*process* (that hosts the activity) be killed too?

Since I saw the concept of an "empty process" in the docs, I suppose
that the answer is no. That is, the destroying of an Activity does not
mean the process will be destroyed too. Therefore, it is possible that
my process is running (even with non-null static variables in it, and
memory leak if I wasn't careful), but it hosts zero Activity
instances.

My second question: assuming I'm right above, the following should
also be true:
a.) values of member variables of my Activities must be considered
invalid after Activity.onDestroy(), even if the hosting process was
not killed
b.) since static variables belong to the *process* and not to the
Activity (even if they're defined in the class of an Activity), their
values are retained even when all Activities were killed (but the
process was not killed)
c.) I should never rely on point b) though. Instead, in the
Activity.onCreate methods, I should re-initialize these static
variables.

Dianne Hackborn

unread,
Apr 23, 2012, 10:11:09 PM4/23/12
to android-...@googlegroups.com
Ignore empty processes; I think that is a rat hole you are getting into.  They are not relevant for understanding this.

First of all, let's poke in to "onDestroy() is not guaranteed to be called when

the activity (or even the process) is killed"

Only processes are killed.  Activities may be destroyed for various reasons, while the process is still running.  You will always get onDestroy() called in this case.  That is how you know the activity was destroyed.

Processes are kept around until their resources are needed elsewhere.  There are various inputs used to decide what order processes will be killed in, based on what is running in it.  In no case does stuff no longer running in a process mean there is an guarantee that the process will now be killed.  It all depends on what else needs the memory.

Re your specific questions:

a.) values of member variables of my Activities must be considered
invalid after Activity.onDestroy(), even if the hosting process was
not killed

You should consider the entire Activity as invalid once you return from onDestroy().  Nothing should be holding references on it, nothing should try to touch it again.
 
b.) since static variables belong to the *process* and not to the
Activity (even if they're defined in the class of an Activity), their
values are retained even when all Activities were killed (but the
process was not killed)

Yes.

c.) I should never rely on point b) though. Instead, in the
Activity.onCreate methods, I should re-initialize these static
variables.

Well, you shouldn't *rely* on the variables being kept for your app to behave correctly.  If you go into onCreate() and those variables are already set, then there is not any specific resource why you need to re-initialize them.

A simple example: you may have a static of the data from an XML file that you loaded from persistent storage.  You only need to do this once.  You may do it in your onCreate(); but if you that static was already set to the loaded data, there is no reason to clear and load it again.

--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Tamás Kovács

unread,
Apr 23, 2012, 10:21:48 PM4/23/12
to android-platform
Thanks for the clarifications. Together with Christopher Tate's help,
all my doubts are clear now except one little thing:

Let's assume activity "A" starts activity "B" (in the same process,
same app), and "B" is in the foreground now. Is it possible for "A" to
get an onDestroy while "B" is active (foreground)? Because in that
case, when "B" finishes (and returns its result to "A"), "A" will be
recreated i.e. will start from onCreate().
> hack...@android.com

Dianne Hackborn

unread,
Apr 24, 2012, 4:33:19 PM4/24/12
to android-...@googlegroups.com
2012/4/23 Tamás Kovács <falcon.f...@gmail.com>
Let's assume activity "A" starts activity "B" (in the same process,
same app), and "B" is in the foreground now. Is it possible for "A" to
get an onDestroy while "B" is active (foreground)? Because in that
case, when "B" finishes (and returns its result to "A"), "A" will be
recreated i.e. will start from onCreate().

You shouldn't assume it can't happen.  If nothing else, you can do this:

1. Start A.
2. Start B.
3. Go the background.
4. Let the process be killed.
5. Return to your app.

Now B is created, and A is not yet created, and when the user presses back A will be created.

--
Dianne Hackborn
Android framework engineer
Reply all
Reply to author
Forward
0 new messages