Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Lifecycle - ActivityManager kills process -> Exception when returning from background

I'm having a very boring problem. When my app goes to background, sometimes the process is killed as the LogCat shows:

INFO/ActivityManager(187): Low Memory: No more background processes.
INFO/ActivityManager(187): Process com.app.myapp (pid 20681) has died.
INFO/WindowManager(187): WIN DEATH: Window{40592708 com.app.myapp/app.myapp.dashboard.Dashboard paused=false}
ERROR/InputDispatcher(187): channel '408b1d40 app.myapp/app.myapp.menus.products.Promotions (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
ERROR/InputDispatcher(187): channel '408b1d40 app.myapp/app.myapp.menus.products.Promotions (server)' ~ Channel is unrecoverably broken and will be disposed!
INFO/WindowManager(187): WIN DEATH: Window{408b1d40 app.myapp/app.myapp.menus.products.Promotions paused=false}
INFO/ActivityManager(187): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10100000 cmp=app.myapp/.Main } from pid 187
INFO/ActivityManager(187): Start proc app.myapp for activity app.myapp/.menus.products.Promotions: pid=20721 uid=10062 gids={3003, 1015}

When i then try to return to the app, via task switcher (long press home) it tries to return to the last opened activity... and crashes.

Why doesn't it open from scratch, opening Main as the whole app was closed?

The ERROR/InputDispatcher erros do not always appear.

UPDATE:

The problem is... the Application is restored, but the objects in it that were downloaded (as a hashmap) aren't... so when i access them.... i have "problems"...

UPDATE 2:

I've manage to fix the problem by verifing on onCreate on BaseActivity (that is extended by every other) if the Application contains the data I need or if it's null (in this case it restarts the app).

Is there a more elegant solution, or at least a way to say to the device that if the application is killed, i want the application to be restarted?

This links helped understand the problem... and knowing there is no clear answer for how to store the data from Application: How to declare global variables in Android?

UPDATE 3:

So I asked a question specifically of how to save Application data correctly. This may help those with the same question: Android: Best way to save data stored in Application Singleton Class

like image 421
neteinstein Avatar asked May 18 '11 15:05

neteinstein


People also ask

How do I know if my app is destroyed android?

there's no way to determine when a process is killed. From How to detect if android app is force stopped or uninstalled? When a user or the system force stops your application, the entire process is simply killed. There is no callback made to inform you that this has happened.

What is the role of an Activitymanager in an Android app?

This class gives information about, and interacts with, activities, services, and the containing process. A number of the methods in this class are for debugging or informational purposes and they should not be used to affect any runtime behavior of your app.

What happens when Android decides to shut down a process?

Android might decide to shut down a process at some point, when memory is low and required by other processes that are more immediately serving the user. Application components running in the process that's killed are consequently destroyed.


1 Answers

As far as I ubderstand it goes back because the user didn't close the application. A though: are you accessing bundle data in that activity that's passed from the opening activity? If so you may want to look into the bundle instance saving and restoration functions that you can implement to solve the app coming back into the foreground in am invalid state.

Edit for clarity: the application is restored to its previous state because it was killed by the OS due to low memory while it's in the background. Upon task switching back it's restored to that previous state and invalid / non-existent bundle data may be causing the crash when that activity is resumed.

like image 54
jlindenbaum Avatar answered Nov 11 '22 10:11

jlindenbaum