0

I have an activity A that is either started from another activity B or via PendingIntent from a Notification, which is created in a service. The notification is shown regardless of the activity being started from another activity or not. A has a button which makes the activity close itself.

I now want to implement the following behavior:

  1. If the activity A is run from B and the user touches the button, the app should return to B.
  2. If the activity A is run from B and the user touches the notification, the running activity A should be shown. If the user touches the button, the app should return to B.
  3. If the activity A is not run from B and the user touches the notification, A should be started. If the user touches the button, the App should be completely closed (except for Services / BackgroundReceivers running in the background).

What flags do I need to set / how do I start the Activity? Currently, activity B starts the service which creates A and the notification. The reason for this is that A might also need to be created from a BroadcastReceiver, which starts the service. Is this the right way?

Matthias
  • 4,481
  • 12
  • 45
  • 84
heyarne
  • 1,127
  • 9
  • 33
  • Try Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK when launching Activity A. Also do not set parent activity for Actiivty A, this will set the default behaviour of back button. – Vijay Jun 09 '16 at 17:14
  • I tried that, but when call `finish()` in `A` in scenario 3, the activity just gets minimized. I want to actually close the application. – heyarne Jun 09 '16 at 17:33
  • Then try calling finish() on button click. – Vijay Jun 09 '16 at 18:02
  • That is exactly what I do, yet the activity does not close. It only minimizes and can be opened again. Is there any good way to inspect the `BackStack` to debug this? – heyarne Jun 10 '16 at 02:52
  • Looks like you want to remove your app to remove from android app history stack? This can be done by ActivityManager.RunningTaskInfo but it is not a good way as you are changing default behaviour here. – Vijay Jun 11 '16 at 10:40

1 Answers1

1

Ok, as far as I understood, you want to know how to handle the button in A activity. if it came from a notification, you finish the app when the button is clicked. If it was already there, you just finish the activity.

I would say to check this. So, you can use the onNewIntent method to handle it. The flag to update will handle when A is already running.

Community
  • 1
  • 1
Renan Bandeira
  • 3,238
  • 17
  • 27