2

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?

nicolallias
  • 1,055
  • 2
  • 22
  • 51
Yunus METE
  • 21
  • 2
  • Possible duplicate: https://stackoverflow.com/questions/6514868/how-to-send-application-to-background – Ricardo Costeira Feb 12 '19 at 13:43
  • Don't think so. I'm not looking for catching home key or sending the app background via another intent. – Yunus METE Feb 12 '19 at 13:47
  • Well, I'm not sure it'll work since I never tried it, but have you tried changing to FLAG_NEW_TASK and FLAG_CLEAR_TASK? The first one creates the activity as a new task (even if you already have another activity stack on background), and the second destroys the previous stack. – Ricardo Costeira Feb 12 '19 at 13:57
  • Have you tried `intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);` – Abid Khan Feb 12 '19 at 13:57
  • @RicardoCosteira adding FLAG_CLEAR_TASK flag sends the app to the background but the problem is the app cannot continue the recent activity of the app after tapping in the recent apps screen. After finishing Acvitiy A and resuming the app from recent app screen, the app shows the initial screen. – Yunus METE Feb 13 '19 at 06:52

1 Answers1

0

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.

Abid Khan
  • 2,451
  • 4
  • 22
  • 45