Launch Activity from a service

Launch Activity from a service

If we have a service in background and we need to launch an Activity in foreground, we need to add the tag FLAG_ACTIVITY_NEW_TASK to the Intent.

// MyService.java

public class MyService extends Service {

  private void launchActivity() {
    Intent intent = new Intent(this, MyActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    this.startActivity(intent);
  }
  
}