Application loosing data when in background for long period

277 views Asked by At

I have an aplication in which I fill a listview from database (greendao). I have screen orientation support, so it works in both orientations. The problem I have is that when app is the background for a longer period of time (cant find some certain point when this happens) and I go back to it, the listview is empty and it crashes if I try to do anything.

I am having an impossible time debugging this since it happens only when application is in the background for some time.

I fill the listview by filling an arraylist from database and then sending it to the adapter.

Does anyone have an idea why this is happening?

EDIT: broadcast reciver problem solved

But the reason why listviews are empty is still not found.

I got another logcat tho (the problem is getting data from database :/ ):

11-11 13:45:31.136: E/AndroidRuntime(4468): FATAL EXCEPTION: main
11-11 13:45:31.136: E/AndroidRuntime(4468): java.lang.NullPointerException
11-11 13:45:31.136: E/AndroidRuntime(4468):     at si.comtron.tronpos.content.DatabaseHelpers.viewArticleWithPrice(DatabaseHelpers.java:70)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at si.comtron.tronpos.ArticlesFragment.onCreateView(ArticlesFragment.java:269)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:831)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1037)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at android.app.BackStackRecord.run(BackStackRecord.java:635)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1399)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:428)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at android.os.Handler.handleCallback(Handler.java:615)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at android.os.Looper.loop(Looper.java:155)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at android.app.ActivityThread.main(ActivityThread.java:5511)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at java.lang.reflect.Method.invokeNative(Native Method)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at java.lang.reflect.Method.invoke(Method.java:511)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
11-11 13:45:31.136: E/AndroidRuntime(4468):     at dalvik.system.NativeStart.main(Native Method)
4

There are 4 answers

2
SweetWisher ツ On BEST ANSWER

IntentReceiver si.comtron.tronpos.USB.USBService$1@4197ac28 that was originally registered here. Are you missing a call to unregisterReceiver()?

It seems you are using BroadcastReceiver

Unregister your receiver in onPause():

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(yourReceiver);
}
2
Alexander On

Go to Developer Options on your phone and check the checkbox Don't save Activities. This way any of your Activities gets killed as soon as it's in background and you, most probably, will be able to recreate the issue without waiting.

0
Payal On

You can create a file that contains all your data, In OnCreate of your activity you should check whether your variables have data.If not , you can just pull the data from the files that you have saved previously.

6
CommonsWare On

Does anyone have an idea why this is happening?

Your app's process was terminated due to low memory conditions, but it still existed in the recent-tasks list. When you try returning to the app, Android forks a fresh process for you and starts up the last activity that you were on... and you are assuming something else in the process should already be around, when it is not.