Starting Activity With Intent Flags Stops Service After It Starts

905 Views Asked by At

In my app, there's an activity which leads to another through an intent with

Intent intent = new Intent(MainActivity.this, OtherClass.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startService(new Intent(MainActivity.this, Service.class));
startActivity(intent);

The problem is the flags set on the intent makes it so that immediately after the onStartCommand() function is called in the service class, the onDestroy() function runs in the same service class. I need the flags set on the intent so that the user can't back into the MainActivity. Does anyone know how I can achieve this effect without having the service stop?

2

There are 2 best solutions below

0
On BEST ANSWER

I ended up calling finish() which, when used when starting intents, achieves the desired effect.

1
On

Try running the service in another process.

add this in the manifest

<service
            android:name=".MyService"
            android:enabled="true"
            android:process=":my_process" />