0

I edited the question so it's not a duplicate of this

In MainActivity I am doing some file operations. These operations are processed in a single file. So, after that I pass the file through intent at ForceShut. This is because I want to detect when user swipes the app out from recent apps which is to say onTaskRemoved() is called, into which this file is deleted. Now no problem so far. The file is successfully transmitted through the intents, and onTaskRemoved() is called as seen from the Logs. Also the file which I try to delete in onTaskRemoved() is successfuly deleted when I swipe the app and the Logs in there all run fine until the "Application Terminated" shows. But few seconds after, I get a crash alert saying App has stopped while app was even removed from the recent. The crash though appears twice in a row then no crashs appear further on. What could be the problem ? :(

My MainActivity class looks like this

public MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //several file operations go here <--
    //removed for simplification 
    Intent mIntent = new Intent(this, ForceShut.class);
     mIntent.putExtra("file", file);
    startService(mIntent);
}
}

and ForceShut class looks like this :

public class ForceShut extends Service {
     File file;

     @Nullable
     @Override
     public IBinder onBind(Intent intent) {
         return null;
     }

     @Override
     public int onStartCommand(Intent intent, int flags, int startId){
         file =(File) intent.getExtras().get("file");
         return START_STICKY;
     }

     @Override
     public void onTaskRemoved(Intent rootIntent){
         if(file.exists())
             file.delete();
     }
}

EDIT

So as @CommonsWare was suggesting I had forgotten to look at the LogCat, instead I was looking only at the "Run" tab logs. So I looked it over and it seems like there is a null pointer exception : Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference so it seems like even after swiped onStartCommand is called again. Why could the service start over even after app was swiped ?

EDIT 2 Question is not a duplicate, as @Andreas has pointed out. I edited the question. However I found the workaround myself. I shut down the Service with stopSelf() as it seems like swiping the app out of Recents sort of doesn't get rid of the service which ocasionally restarts. Anyway hope this helps anyone

ndricim_rr
  • 27
  • 8

2 Answers2

0

Why could the service start over even after app was swiped ?

You have started a "sticky" service. The system will automatically restart any sticky service until it is explicitly stopped.

 @Override
 public int onStartCommand(Intent intent, int flags, int startId){
     file =(File) intent.getExtras().get("file");
     return START_STICKY;
 }

I don't see where you actually stop it with stopSelf().

As far as your NullPointerExceptions, simply check if the objects exist before trying to read from them.

if (intent != null && intent.hasExtra("file"))
    file =(File) intent.getExtras().get("file");
Sam
  • 86,580
  • 20
  • 181
  • 179
  • I tried stopSelf() but didn't edit the code so others could see the mistake. Thanks for your answer. It explains the issue. – ndricim_rr Jan 22 '18 at 20:28
0

The service restarts because of the START_STICKY inside onStartCommand.

You need to use START_NOT_STICKY instead to prevent the service from restarting.

lakshman.pasala
  • 565
  • 6
  • 16