简体   繁体   中英

Application loosing data when in background for long period

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)

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.

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);
}

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.

您可以创建一个包含所有数据的文件,在活动的OnCreate中,您应检查变量是否包含数据。如果没有,则可以从以前保存的文件中提取数据。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM