• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Prevent app from being destroyed when android goes into sleep mode

id0001

Well-Known Member
Apr 20, 2010
142
23
Netherlands
I am developing some sort of web image viewer which gets a list of images from the web and shows it to the user.

My problem is that when android goes into sleep mode by waiting for the screen to turn black or by pressing the powerbutton, my app gets destroyed and reloads when android awakens again. It is then reset and needs to reload all the images I downloaded earlier.

When I download the images I put them in the cache and add a string id to a buffer class so the app can download images async in the background and the user can quickly navigate through images.

Is there a way to prevent my app from being destroyed when android goes to sleep mode?

Thanks in advance.
 
Hi,
I'm brand new to coding on Android and in general, but I've been reading the docs for the past several hours and I think this is the answer to your problem. I'm not sure if it works on sleep, but it sounds like it should:

Application Fundamentals | Android Developers

If the user leaves a task for a long time, the system clears the task of all activities except the root activity. When the user returns to the task again, it's as the user left it, except that only the initial activity is present. The idea is that, after a time, users will likely have abandoned what they were doing before and are returning to the task to begin something new.

That's the default. There are some activity attributes that can be used to control this behavior and modify it:

The alwaysRetainTaskState attribute
If this attribute is set to "true" in the root activity of a task, the default behavior just described does not happen. The task retains all activities in its stack even after a long period.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones