I got an app which can receive an event (which is basically a message from a service in the background), and when it does, I am invoking this code:

```
Intent dialogIntent = new Intent(this, MainActivity.class);
                dialogIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(dialogIntent);

```

This code is supposed to return the app to the front so the user can handle the event.

This code works only if the app I'm working on was also the last app the user has been in. The code doesn't work in these scenarios:

1) The user has paused the app, opened another app and then paused it too so the 2nd app is the last in the background - in which case the last app will always open, even though I'm starting my `MainActivity`. For example, if I've moved my app to the background, opened Android's settings and then an event is received, the Android's settings screen will open instead of my app.

2) The user is inside another app. In which case, the app won't be brought to the front at all.

How can I made the specific app to be resumed from the background no matter what?