Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android service crashes after app is swiped out of the recent apps list

Tags:

I have a service that gets started (not bound) by an activity. If the activity gets destroyed (e.g. by pressing the back button), the service continues to run, this is of course intended. However, if I swipe the activity out of the 'recent apps' list, the service gets restarted immediately. This is reproducible, every time the activity/app is swiped out of the list, there is a new call to the service's onCreate-method. No call to onDestroy in between!

First I thought the service gets killed by android, even though I saw no reason for the kill (neither the activity nor the service do resource consuming things, in fact they are minimalistic and do nothing). But then I noticed that the service actually crashes.

V/MainActivity(856): onDestroy // swipe out of the list I/ActivityManager(287): Killing 856:com.example.myapp/u0a10050: remove task W/ActivityManager(287): Scheduling restart of crashed service com.example.myapp/.TestService in 5000ms 

The code is not noteworthy, but here it is

Activity:

public class MainActivity extends Activity {      private static final String TAG = "MainActivity";      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         Log.v(TAG, "onCreate, starting service...");         startService(new Intent(this, TestService.class));     }      @Override     protected void onStart() {         super.onStart();         Log.v(TAG, "onStart");     }      @Override     protected void onDestroy() {         super.onDestroy();         Log.v(TAG, "onDestroy");     }      //[...] } 

Service:

public class TestService extends Service {      private static final String TAG = "Service";      // onBind omitted      @Override     public int onStartCommand(Intent intent, int flags, int startId) {         Log.v(TAG, "onStartCommand");         return super.onStartCommand(intent, flags, startId);     }      @Override     public void onDestroy() {         super.onDestroy();         Log.v(TAG, "onDestroy");     }  } 

In short: My service is independent of the activity's lifecycle, but only as long as I don't swipe out the app of the recent apps list. In that case, the service gets restarted but without a call to onDestroy.

Every time this happens, not only the state of the service, but also the work the service is doing is lost. I just want to know why the swipe is the reason for this.

like image 963
alapeno Avatar asked Sep 04 '13 11:09

alapeno


People also ask

What causes app crashes on Android?

Apps on Android can crash because of low storage space, too many apps running simultaneously, a weak internet connection, or not having the proper app updates installed.

How do you fix an app if it keeps crashing?

To fix Android apps that keep crashing: To do this, go to Settings and open Apps. Under Your apps, you'll see a list of the apps currently installed on your device. From the list, tap the app that keeps crashing and tap Force stop in the bottom right corner. Then try opening the app again.

Why do my apps crash randomly?

This usually occurs when your Wi-Fi or cellular data is slow or unstable, causing apps to malfunction. Another reason for Android apps crashing can be a lack of storage space in your device. This can occur when you overload your device's internal memory with heavy apps.


1 Answers

Swiping the app from the recent tasks list actually kills the operating system process that hosts the app. Since your service is running in the same process as your activities, this effectively kills the service. It does NOT call onDestroy() on the service. It just kills the process. Boom. Dead. Gone. Your service does not crash.

Since your service returned START_STICKY from the call to onStartCommand(), Android recognizes that your service should be restarted and schedules a restart of the killed service. However, when your service is restarted it will be in a newly created process (you can see onCreate() called in the service), so it will have to start the work all over again.

Rule #1: Don't ever swipe apps from the recent tasks list ;-)

like image 70
David Wasser Avatar answered Nov 11 '22 10:11

David Wasser