Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App crashes when restoring from background after a long time

Tags:

android

My application crashes after idling for a long time in the background. I debugged and found the reason that it is crashing is due to a NullPointerException. Exception was due to the fact that the data in the application singleton class is destroyed by the garbage collector when application is in background. I am using static data in each activity throughout my application.

My question is: is there any way to make the data of application class persistent when in background? Or is there any other solution?

like image 206
Qamar Zaman Avatar asked Apr 12 '13 06:04

Qamar Zaman


People also ask

Why are my Apps crashing in the background?

This usually occurs when your Wi-Fi or cellular data is slow or unstable, causing apps to malfunction. Another reason for Android apps crashing can be a lack of storage space in your device.

Why does Android Studio keep stopping Apps?

Improper installation can cause Android Studio to keep stopping. When apps are installed on a device, all the necessary files to run the app will be stored on the device. Once any required files don't store on the device, the app will not work properly and may keep sudden stops.


3 Answers

For more accurate answer you put your code here. On, Android memory is limited so VM can remove any piece of code it think un-necessary.

Look into Activity life-cycle method, specially into onResume and make sure that you understand that perfectly. So many time application crashes just for not using Activity life-cycle method properly.

Another important design consideration for Activity is, no matter what happened with persistence data you Activity should display its UI with some default value. So assumption is like this, if i have data i will display if i don't, i really do not care. Your UI should never ever crash with or without data. You can use String.xml for storing some default value or even in layouts.

if you still want go with singleton class, which is perfectly fine but make sure you do the following checking every time you try to access your singleton.

if (instance==null)
    instance=Singleton.getInstance()

your getInstance() method not only return you current instance it will also make sure that

  • it initializes all object and variable
  • other singleton methods as instance method

Do not statically access data from one Activity to another. It is not good for android specially for the type problem you are facing now and also it is not very good OOP programming practice.

SharedPreference is good way to persist data, if that meet your requirement go for it.

if you want to pass data from different Android component like Activity, Service or BroadcastReciever you can put it inside a bundle and and send as intent. And, as always their are SQLLite data storage, file IO etc etc.

like image 138
minhaz Avatar answered Oct 24 '22 01:10

minhaz


There are several different ways to save data. If it's something small you could use SharedPreferences. Otherwise perhaps an SQL database?

Check out http://developer.android.com/guide/topics/data/data-storage.html

like image 45
David Olsson Avatar answered Oct 24 '22 03:10

David Olsson


If you can, store it in other methods. If its complex data structures, you may have to restart the app. For example, I have a User singleton in my app that holds login info for my server. I couldn't save those if I tried- a lot of the data is hidden in AWS variables. What I had to do was detect when we were being relaunched from the home page after being cleaned up (as opposed to just being restarted) and launched my starting activity, then finished the current activity. And I had to add that to every activity in my app.

like image 36
Gabe Sechan Avatar answered Oct 24 '22 02:10

Gabe Sechan