0

I got an Android application with a service. If the app is minimized but is still in the background, I have no problem returning it to the foreground with a startActivity call. However, this call does not work if the app was swiped off from the recent apps list.

How can I reopen the app from a service if it was swiped off?

JJ Ab
  • 492
  • 4
  • 15
  • why would you want that – Tim Jan 24 '17 at 13:30
  • while the good way is push up notification from there you start your activity upload some code – Nouman Shah Jan 24 '17 at 13:31
  • @TimCastelijns the app handles voip calls and I want to open it when there's an incoming call – JJ Ab Jan 24 '17 at 13:33
  • how certain are you that the issue is that startActivity is not working? Perhaps the issue is that the service is not running – Tim Jan 24 '17 at 13:36
  • It says so in the running apps section in the Androids settings menu. However after looking at the code, its a bit more complicated. The service is in a library project which the app uses. When there's an incoming event, the service calls for a listener which runs the code above but it calls it in the application's main activity (since the service doesn't know the activity of the application itself which is in separate project). Will it work if the app was swiped off? – JJ Ab Jan 24 '17 at 13:50
  • Please explain what you mean by _"the service calls for a listener which runs the code above but it calls it in the application's main activity"_ Post some code to show how this works. If the `Activity` has passed a reference to a listener, then after the app is "swiped" (killed), this listener won't exist and the `Service` will have been restarted (if the `Service` runs in the same process as the UI components (activities). Post the manifest also please. – David Wasser Jan 24 '17 at 16:29

1 Answers1

0

According to this answer you will need to use the Intent.FLAG_ACTIVITY_NEW_TASK flag. If there is no open task with the Activity you want to open a new one will be created.

Starting the activity would look like this:

Intent intent = new Intent(this, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

documentation

Community
  • 1
  • 1
schrej
  • 450
  • 3
  • 10
  • As I said, its only working when the app is minimized, but not if its closed – JJ Ab Jan 24 '17 at 13:36
  • Are you sure your service is still running after you close the app from the history? – schrej Jan 24 '17 at 13:37
  • It says so in the running apps section in the Androids settings menu. However after looking at the code, its a bit more complicated. The service is in a library project which the app uses. When there's an incoming event, the service calls for a listener which runs the code above but it calls it in the application's main activity (since the service doesn't know the activity of the application itself which is in separate project). Will it work if the app was swiped off? – JJ Ab Jan 24 '17 at 13:50