1

I am new to android development. I made an application on android but i am facing a crash after my app long sleep in the background.i tried singletons and sub application but both of them leads me to a crash after long sleep.

The configuration that i am saving is a list of objects many of them.also these configuration should be accessible in the whole application.

any Idea how to handle this? I'd searched the websites looking for answer but with no luck.since the Shared preference only for String,boolean ,integer and floats....also I am targeting android 2.1 and higher.

Is there any Easy procedure to that avoiding me to change the whole application mechanism ?

Thanks

Ahmad Hariss
  • 27
  • 2
  • 9
  • you can use a sqlite database to store all of your object in a table, – Houcine Jan 25 '12 at 14:14
  • You have to persist this information one way or another. File, Sqlite and SharedPreferences are the standard options, and there is sample code all over the web for all of these – Rich Jan 25 '12 at 14:44
  • yes it would be easy if i used sqlite or shared preference for object contains simple vars, but what about if i have a list in my object? i need then to design a complext database for this, thats why am running away from database or shared preferences. what i am doing now fillowing stefan post, and checking if the database null then i am sending him back to the splash screen ti re-initialize the data again...i think this is also usefull for login. my experience in java is limited since i used to work in web applications php.so its little confusing for me :) – Ahmad Hariss Jan 25 '12 at 21:11

1 Answers1

0

I have had bad experiences with Singletons in Android. The problem is that the Singleton can outlive the application, if the application is destroyed but the jvm is not the Singleton's state is not reinitialized when the activity is restarted in the same jvm, i.e. with the same instance of the singleton (connected to the old class loader). Does this explain the behavior of your application.

A possible solution is to use the the Application instance created by Android. Its life cycle is managed by android and you can manage the state in onCreate(), ... Since API level 14 there are also activity life cycle callbacks. Look here for more information: http://developer.android.com/reference/android/app/Application.html#registerActivityLifecycleCallbacks%28android.app.Application.ActivityLifecycleCallbacks%29

If I need a global variable and the database or shared preferences are not appropriate solutions I usually use the Application instance to store the state. You can get the initialized Application or Service instance by calling

(MyApplication) this.getApplication();

in your activity or service, e.g. in onCreate(). You initialize your instance of MyApplication (which extends Application) in its onCreate() method.

Stefan
  • 4,645
  • 1
  • 19
  • 35
  • well kinda thats was helpfull, since i tired alot of solutions i went lost what is the approperate one to use. am running away from saving my data into the database,and my app configuration are objects and list of objects,like this: public class MainObjs extends Application { private List Weather=null; private List SearchResult=null; private List Directories=null; } and each on these has setter and getter,is there any reference how to use (MyApplication) this.getApplication(); with my dynamic objects?how to access and how to set them? – Ahmad Hariss Jan 25 '12 at 15:11
  • Thanks for your reply stefan, This is exactly what i am doing, but the data inside the application filled dynamically, and when the app sleeps they are being cleared null exception error, which i guess Directories is being set to null,i should re initialize them again? is that right or i miss understood it? – Ahmad Hariss Jan 25 '12 at 19:09
  • Sorry, I do not understand the problem. The data in the Application instance is not touched when the application sleeps. – Stefan Jan 26 '12 at 08:25
  • well, this is the case as numbers: 1-splash activity is main activity that has application "A" instance configured,application object "A" has sets of object equal to null. 2-Splash page on create, get the data from the web browser and populate the objects inside Application Object "A", when the user navigate through the application and send the Main application into background to sleep,once he re-lunch it,its crashes because it doesn't find the data in application Object "A" because it has been killed. – Ahmad Hariss Jan 26 '12 at 08:46
  • You say "re-launch"; so the activity is not only send to background but the application is possibly stopped while the user navigates through the activities of some other application, correct? The application object is not guaranteed to survive indefinitely when every activity/sorvice of the application is destroyed. I suggest to use a database or SharedPreferences in this case. – Stefan Jan 26 '12 at 11:50
  • Re-lunch or press home button too long and access the recent applicaiton. i guess i have no other choice than using shared preference.but the point the data is too big ...Thanks Stefan for ur help :) – Ahmad Hariss Jan 27 '12 at 08:15