How to send app to background after finishing activity which is started from push notification

264 Views Asked by At

I have an activity which is called if the app receives a push notification. The activity is started with FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TOP. The activity, let's call it 'A' shows UI and finishes after a while. In this point have a problem with activity stack.

Scenario:

  1. The app is in the background with another activity 'B'
  2. Then the app receives a push notification and starts Activity A.
  3. After related things done, the app finishes Activity A
  4. Then returns to Activity B and stays in the foreground even the app was in the background before the push notification is received.

After debugging, I figured out that the system calls onResume method of Activity B after finishing Activity A.
How can I do the app keep staying in background if the app started from background? Should I change intent flags of the activity A?

1

There are 1 best solutions below

1
Salman On

In your case you can achieve this in two ways

1- From manifest file with activity tag android:noHistory="true"

2- From code when you are staring the activity set flags like below

Intent mIntent = new Intent(context, Youractivity.class); 
mIntent.setFlags(mIntent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(mIntent);

For more information checkout developers link

One other thing you can do is instead of this.finish() in notificationActivity is to use this.finishAffinity();. This will close the app instead coming to foreground.