how to force android system to recycle all background activities? - android

Is there API or commands to force Android system to recycle all background activities no matter there is enough resource or not? And how to check all the activities' status to check that the activity is actually killed?
There is an API called killBackgroundProcesses(), but this API is killed the whole process, I am wondering how to only kill some activities without killing the whole process.
As the android dev guide page says below, I am looking for the first way.
activity lifecycle
If an activity is paused or stopped, the system can drop the activity
from memory by either asking it to finish, or simply killing its
process.

Yes, there is a way:
Settings -> Developer options -> Apps -> Don't keep activities
Check that box, and you are good to go.
Cheers.

I don't think so Android gives you the information of Activities are in background (i.e in Paused or Stopped State). Even if Activity Stack as well will give you the access of activity at Top.
Possible Solutions :
1. If at all you want to destroy the service , better to call finish() after startActivity() method.
2. If you want to periodically destroy all the background activities. You should implement your own activity stack. Which does pushToStack() on start of new activity and popFromStack() and then activity.finish();

Related

When does Android destroy activity without destroying the entire process?

I want to understand and simulate when does Android call onDestroy() of my activity, without destroying the entire process. I'm not calling finish(), and I want to make Android destroy my activity on it's own.
From the activity-lifecycle documentation:
The system never kills an activity directly to free up memory. Instead, it kills the process in which the activity runs, destroying not only the activity but everything else running in the process, as well.
But the Android Activity documentation says:
This (onDestroy) can happen because [...] the system is temporarily destroying this instance of the activity to save space.
So which one is it? Does Android destroy activities when it's low on memory, or does it only kill entire processes?
I want to simulate a situation where Android kills the activity without killing the entire process.
I can mimic this by using the "Don't keep activities" developer-mode setting, but I want to understand how can this happen in the real-world.
I want to make Android destroy my activity on it's own.
Android does not do that, other than through your actions (e.g., finish()) or user actions (e.g., BACK navigation, configuration changes).
So which one is it?
The former (Android kills processes, not activities). See this answer from the woman who wrote this stuff. FWIW, also see this decade-old blog post of mine.

How an Activity is killed by Android?

As we know Android would kill a paused Activity in some conditions. And there is a FIFO back stack of Activities.
My questions are as follows:
How Android kills an Activity without pulling it from the back stack (it might affect the top active Activity)
After killing it, what things are released from this Activity? And can I still get this Activity's instance?
Android does not kill Activities "separately", it kills the whole app process with all Activities.
The only way to get an Activity killed by the system is to set Don't keep Activities flag in device's Developer Options. However this option is just for development, not for applications in release.
Activity can't be killed but Os can kill the whole application. In this case you can try finish()/finishActivity()/context.finish() to finish the activity. While you finish an activity backpress will not return to the previous activity.
System can't kill activity, it can call the whole app.
And when it kills the whole app, it doesn't call any method from activity (onStop(), onDestroy(), etc)
Finally, you can't get activity instance.

How to force Android to trigger activity kill

I am having an issue that's closely related to
support FragmentPagerAdapter holds reference to old fragments
and ViewPager and fragments — what's the right way to store fragment's state?
Anyway, my problem is that my application crashes on this one activity when it gets recreated after the system kills it. As it's a pretty heavy activity and I'm debugging to implement fixes, I need to trigger the "Activity killed by android system".
Right now, I am doing "Open 20 other apps, and then hope that my app gets killed before reopening it".
Is there any better way?
PS: I have tried killing it manually (force killing) from app information. It doesn't work, as my application gets recreated from my home screen
Actually I found an answer...
In developers settings, all the way down, look for
App -> Do not keep activities
Tick it, then launch your activity, leave it using homescreen, launch any other app (gallery or whatever), and then when you relaunch your app, it will have been killed by the android system
Calling the finish() method should work.
If you are inside of the context of the activity then simply call finish();
If you are outside of the context. Then pass the context of the activity and call
activity.finish();

Android multitaksing

I am seeking short characteristic of Android Multitasking. I have found, that when you minimize Android application, it is ended and it's process remains on the background. When user wants to reuse his app, this process alive it's application. User will be at the same state, when he left (if there was enough memory while working with it), or it will be loaded from scratch, because there was no free RAM for other work and Android exited this process. Am I right? Everywhere there are articles with 20 pages and more about Android multitaksing. I need to know key points because I am lost in a such long artices.
Thanks
Short Answer: Yes. If your app can live in memory despite being 'closed' then it will stay in RAM and processing will continue when you click on it again. Otherwise it will be restarted and you will get an onResume().
Long Answer: Please just read the Activity Lifecycle:
When Android activity is covered by other windows it will enter into paused state and method onPause will be called. It may also me destroyed by OS and then onDestroy will be called. You have very little control over it and can't expect your application to come back up with the same state. However, when activity is brought up again to foreground in will go through steps of onCreate and onPause. Those methods can be used to restore its state.
Here you can find nice diagrams describing Activity lifecycle. Similar but slightly different lifecycle is applicable to service.
http://developer.android.com/reference/android/app/Activity.html
Android activities are the main visible screens that user see while the application is running. If you close the screen or switch to another application, the current activity is put to hibernate and you can save the state with
Activity.onSaveInstanceState(Bundle bundle)
After your activity gets the control back, you can restore the state with
Activity.onRestoreInstanceState(Bundle bundle)
Note that you need to be careful not to store any context references within the activities and related classes as the activity and thus context has changed between pause and resume. Instead, you should always pass the current activity as the active context to avoid having exceptions from invalid context.

If my application goes to background, it still continue working? (sending/getting data from internet)

i am working on an application that send and get data from internet each 5 min
if i press home key and my app goes to background... it will still continue sending/getting data from internet? or i have to do something special?
thanks
"If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere. If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state."
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
No. Activities shouldn't be depended on to process tasks in the background.
The following link illustrates the fundamentals of different Android components and what they do, i.e. the "parts" of an app.
You should be using a Service for background processing.
From what you're saying, I'd suggest an IntentService fired by an Alarm.
Application Fundamentals

Categories

Resources