Josh
Josh

Reputation: 2725

Start Activity without bringing Application to front

I am working on a lock screen replacement for my media player, and I am having trouble with one aspect.

The lock screen itself is a new activity that is launched by my media service whenever the screen turns off. The problem is that the Activity uses the applications context to launch, which causes it to bring the app to the front after the user unlocks.

I have tried using the services Context to start the activity, but I believe this does not work because the service itself is linked to the application.

Below is the code in the service that launches the activity:

Intent mLock = new Intent(context, LockScreen.class); 
mLock.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mLock);

Does anyone have any suggestions on how I can launch this activity and keep my app in the background?

Upvotes: 8

Views: 4289

Answers (3)

Josh
Josh

Reputation: 2725

After a lot of searching I was able to get the results I desired by adding a FLAG_ACTIVITY_MULTIPLE_TASK. This allows the lock activity to start and end without bringing the parent application to the front.

I will continue to test this solution and post any drawbacks I find. If anyone else would like to chime in on this feel free...I know this flag has drawn criticism in the past.

Thanks, Josh

Upvotes: 3

Philip Pearl
Philip Pearl

Reputation: 1533

In the manifest set the task affinity of the lock screen. That should sort you out without using FLAG_ACTIVITY_MULTIPLE_TASK.

Upvotes: 3

Andrew Anderson
Andrew Anderson

Reputation: 457

I'm a little bit unclear about your question. But I hope I got it right.

In case you want to activate the lock activity and when it is unlocked you don't want to see the activity(say activity X) that was running before the lock activity was called, then close this activity(X) before calling the lock activity.

If you want X to run even when the lock activity is active, but closed when it is unlocked, then use flags. Set a flag once it is unlocked and allow X to periodically check for this flag status.

Hope I was able to help you.

Upvotes: 0

Related Questions