gop
gop

Reputation: 2200

Bring application to background and then to foreground

What method should I use to move my app to background and then move it to foreground again? I tried using moveTaskToBack(true) and the activity is moved to background successfully but then I can't move it to foreground. I tried starting the activity again using startActivity() but with no success and there seems to be no method moveTaskToFront() or something similar.

Upvotes: 6

Views: 8081

Answers (2)

Basavaraj Burge
Basavaraj Burge

Reputation: 1

Thanks it worked for me by adding following intent

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

Upvotes: 0

David Wasser
David Wasser

Reputation: 95578

Use moveTaskToBack() to move your app to the background.

To move it to the foreground, use the following code:

Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
startActivity(intent);

If you are trying to do this from a Service or BroadcastReceiver then you will need to do this before calling startActivity():

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Upvotes: 13

Related Questions