persistent class variable

Put your problem here if it does not fit any of the other categories.

persistent class variable

Postby j2m3 » Fri Jan 09, 2009 12:30 pm

Hi everyone!!!

When I'm restarting my application in emulator the class variable (where I remeber if someone pressed back button I use it to finish all the threads) has my previous launch value! How it is possible? Is android uses old class data to create new one? Can anyone explain me that?

Thanks,
AA
j2m3
Freshman
Freshman
 
Posts: 6
Joined: Fri Jan 09, 2009 12:24 pm

Top

Postby MrSnowflake » Fri Jan 09, 2009 5:19 pm

Depends on how you store the presistent data. It could be a VM bug, but I think that's a slim chance. If you use SharedPreferences for this, then keep in mind you settings stay the same between installs/deployments!
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby j2m3 » Sat Jan 10, 2009 8:28 pm

I'm not storing anything,
the variable is static :
public static boolean keyBackPressed = false;
if someone press the back key I change it to true.
You can try it , the same is on the g1 device!!!!
j2m3
Freshman
Freshman
 
Posts: 6
Joined: Fri Jan 09, 2009 12:24 pm

Postby Quetzalcoatl » Sun Jan 11, 2009 12:07 am

Static variables are held in the same memory space and are common to all instances of the same class. So, as the device is not reset is it just pulling out the old data at this memory space when the app restarts? (or is Java safer than C++?)
Image
Quetzalcoatl
Senior Developer
Senior Developer
 
Posts: 129
Joined: Sat Oct 18, 2008 8:21 pm
Location: near Manchester, UK

Postby MrSnowflake » Sun Jan 11, 2009 11:53 am

Well yeah, you have a good point. When the app actually restarts, a new VM environment will be started, BUT when the app goes to the background and you 'start' it again using the home screen, the original task will be restarted. So you should set keyBackPressed=false in the onCreate(). You should always do this, for the exact reason I just said.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot], Yahoo [Bot] and 30 guests