My Android app's singleton gets re-initialized

2019-09-19 18:05发布

I searched for 2-3 days a solution to my problem, but i haven't found anyone who had a similar problem. Basically, my app is the "mobile version" of a web site, so I have to store temporally different account's informations, so I used a signleton class, call it "user". Yet while navigationg through certain 2 activities (in one I have an expandable list and in the the other a listview , like you have a cart (1st activity) to substitute its empty spaces with elements from the "shop" (2nd activity); so you go from one the other and viceversa several times). So, while going forward and backwork (via Intent()), suddenly the singleton gets re-initialized making the app crashing. The strange fact is that I do not get any error on the logcat, it only logs at a certain point that the singlotn call its init() method, making the app crashing immidiatley after. Another strange thing is that the first time that I implement this stuff it worked wiht no problem, yet now that I've implemented several other activities and added all the graphic elements (backgrounds for activities and list elements, many drawables, etc.) I get this problem (only doing the operations said before). (I don't know if this thing is relevant) My app uses the sliding menu by J. Feinstein (which is impemented in several acitivities, the one with the expandable list, e.g.) and the ActionBarSherlock (yet I use a custom action bar, so I inflate my own actionBar layout). Each activity has its sliding menu, created every time on onCreate() method of each activity.

Every part of code or log events that you need to help me, just ask and I'll put it here.

Thank you very much in advance for your help, I'm really stuck with this problem.

1条回答
祖国的老花朵
2楼-- · 2019-09-19 18:42

I guess you want to have some "session" user.

I don't know the exact issue, but afaik the Android system may destroy static data when it is low in memory.

What you could do is store some values (user, timestamp) as Preference-values or as persistent data (file, db). Another way is of course to pass the required user data from one activity to another.

查看更多
登录 后发表回答