Starting Activity With Intent Flags Stops Service After It Starts

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?

Answer

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

Leave a Reply

Your email address will not be published. Required fields are marked *