Reputation: 2430
I know that android SO can kill activities and process if it needs memory.
Question 1: Can android kill an activity if it is in foreground?
Question 2: Can android kill the activity's process if the activity is in foreground?
Upvotes: 1
Views: 3518
Reputation: 2770
Every process is assigned an OOM score. The score depends on the process class and usually there are other processes to kill before Android OOM killer starts considering foreground processes. But, there are other classes with higher priority than foreground and it is entirely possible that someone (vendor?) stuffed the device with so many memory-hungry persistent processes that OOM has to kill some of the foreground class. Though I would say it is highly unlikely. What does "dumpsys meminfo" say?
Upvotes: 0
Reputation: 3026
The Android processes and application lifecycle document states that:
foreground process
...
There will only ever be a few such processes in the system, and these will only be killed as a last resort if memory is so low that not even these processes can continue to run. Generally, at this point, the device has reached a memory paging state, so this action is required in order to keep the user interface responsive.
Which means your activity (and therefore process) can be killed but only under extreme memory conditions and as a last resort. Empty processes, background processes, service processes and visible processes will all be killed before your process will be killed so it's extremely unlikely this will ever happen but the possibility is there if leaving your application open will lead to system instability.
Upvotes: 3
Reputation: 5600
Answer 1: Yes.
Answer 2: Explain better please.
Resume: You can get all running processes and kill those with the specified pid
Add 2 permissions to the manifest:
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.RESTART_PACKAGES"/>
Later use this code to get PID and later kill process
ArrayList<Integer> pids = new ArrayList<Integer>();
ActivityManager manager = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> listOfProcesses = manager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo process : listOfProcesses)
{
if (pids.contains(process.pid))
{
// Ends the app
manager.restartPackage(process.processName);
}
}
Upvotes: 0