6

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

Community
  • 1
  • 1
neteinstein
  • 17,529
  • 11
  • 93
  • 123
  • http://stackoverflow.com/questions/6025554/android-application-loses-state-after-launching-another-intent/6025590#6025590 – superfell May 18 '11 at 15:39
  • That link ALMOST answers my question.. the problem is that i have data stored in Application class.. that when the Application class is restored are not. – neteinstein May 18 '11 at 16:05
  • This was also useful, but it didn't take my doubt about what method to use: http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables – neteinstein May 19 '11 at 18:44

1 Answers1

3

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.

jlindenbaum
  • 1,891
  • 18
  • 28
  • But what about activity stack history? If i press back after that, even if it doesnt crashes.. it will close right? As it is the only activity opened? Or am i wrong? – neteinstein May 18 '11 at 15:57
  • Edited question to address your answer. – neteinstein May 18 '11 at 15:59
  • http://developer.android.com/reference/android/app/Activity.html#SavingPersistentState read that. It talks about onSaveInstanceState and pausing your app. That's the elegant solution. Detect the pause, save our state. And on resume you have a chance to reinitialize without reloading completely. I'm unsure about the activity history part of your comment. You'd have to try it out – jlindenbaum May 18 '11 at 16:36
  • The problem is.. if you have data that is shared by multiple activities. Then what, save in each the data? That kind of defeats the purpose of using the less memory we can. That's why i store some stuff on the Application, so that every activity can use the data, which is shared. – neteinstein May 18 '11 at 17:09
  • In your state bundle you only save what you need. It's temporary memory. Just a point in time. Once you resume the app that memory is thrown away after the lifetime of the activity and you can go back to using your shared settings values on other activities. Think of these state bundles like an emergency snapshot of data only to get the application back into a useable state when resuming. – jlindenbaum May 18 '11 at 17:44
  • ok, then for each activity i'm supposed to use state bundles to each object on the Application? Am I wrong or are they called everytime the activity stops? – neteinstein May 18 '11 at 17:47
  • Just throw into the bundle what you need before you app is paused / destroyed for memory. It helps if you extend Activity with your own Activity (e.g. "MyApplicationActivity") and then use that for all other activities. Then you can implement cleanup and init methods for data that's shared across different activities. You should only have data active for that activity or in a thread/service. These need to be shut down if you're leaving the app. Most of this is explained in the Activity reference page. – jlindenbaum May 18 '11 at 18:39
  • ok thanks. I'll have to see that question about the stack of activities after the kill... – neteinstein May 18 '11 at 23:59