It is recommended to use Services to achieve that goal, but I've encountered similar situation when in need for something from another app - and the intent for the service wasn't exported, so the only way to start the service is to start the activity - and to keep the user in my app I need to keep my activity in the foreground.
In that case it's possible to start the other activity in an AsyncTask, and then in onPostExecute restart myself.
(new AsyncTask() {
boolean starting = false;
@Override
protected Void doInBackground(Void... params) {
if(!externalServiceRunning()){
Intent externalIntent = getPackageManager().getLaunchIntentForPackage("com.external...");
if (externalIntent != null) {
starting = true;
startActivity(externalIntent);
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if(starting) {
Intent myApp = getPackageManager().getLaunchIntentForPackage("com.my.myapp");
if (myApp != null) {
startActivity(myApp);
}
}
}
}).execute();