2

I have recently encountered a problem where sometimes accessing a singleton from a background Service threw a NullPointerException.

The singleton instance is constructed in the launcher activity and is guaranteed to not be null for the duration of my app.

This leads to my questions of how the Service lifecycle is related to the application process's lifecycle. Concretely, here are the specific questions:

  1. When an activity goes into background, will the containing process be swapped out to secondary storage? If so, how does that affect the service running in the background?

  2. When a service is restarted by the android OS, how much of the containing process will be restored? Will the process just become a wrapper for the service?

Update: Here are the code related to my singleton Implementation

private static User curUser = null;
public static User getCurUser(){
    return curUser;
}
public static void setCurUser(User u){
    curUser = u;
}

There is only 1 place in my app that calls setCurUser(), that is in the launcher activity, which gurantees that the user is non-null before setting it.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Shawn Li
  • 419
  • 3
  • 17

0 Answers0