How to resume Android Activity programmatically from background
In order to bring your app to the foreground, you must call startActivity()
from another context (either a Service
or a BroadcastReceiver
). Just calling startActivity()
from within an Activity
won't bring your app to the foreground.
You don't need the ACTION and CATEGORY in your Intent
, but you do need to set Intent.FLAG_ACTIVITY_NEW_TASK
.
If your activities are on different tasks, you can use this to bring the activity's task to foreground:
ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
activityManager.moveTaskToFront(getTaskId(), ActivityManager.MOVE_TASK_NO_USER_ACTION);
You would need android.permission.REORDER_TASKS
in order to do so. It works even in Android M, however getting an intent from a service works better in some cases.
To Resume application from background programmatically will
Make your activity FOREGROUND, User's current activity in BACKGROUND. User's work is GONE
This is not right behavior from user perspective. Additionally, if we resume activity programmatically, the activity lifecycle will be broken. The activity states will be lost.
Alternative :
You can provide NOTIFICATION
when your timer, task (anything) is complete in background. If user is interested in checking your activity then he/she can check from NOTIFICATION without interrupting current work