My app is retaining all of the variable values when it closes and this is effecting how it runs when reopened. Is there any way to reset them all upon app close? or is there a way to clean the app from memory when it is closed so to speak? For the moment I have just been setting all of the important variables "=0" in the last few lines of execution but I know there must be a correct way to doing this.
EDIT:
OK I thought that it would just be easier to reply here instead of individually to everyone.
The app is indeed staying alive in the background, I checked with advanced task killer. How would I get the ap to "Die" by presing the back button? I think this would be the easiest solution given how the app works:
open app > press start button > press stop button > results screen > press back button to exit.
so basically each time the app runs should be an independant execution.
I like what @Alex and @Jack said. To add to that, also consider that you can call
finish()
in yourActivity
if you want to force it to close up and return to the last Activity. Going along with this, also consider the use ofsetResult(int)
(JavaDoc Here)You can also set a flag on the
Intent
when you call theActivity
you are questioning about. A flag like FLAG_ACTIVITY_NO_HISTORY could be helpful:List of Intent Flags
You need to familiarize yourself with the Activity Lifecycle.
You could leverage
onResume()
to reset your variables; also noteonDestory()
andonPause()
.UPDATE:
Killing the application in its entirety each time the app moves to the background is an anti-pattern. You should really look at your application and follow the aforementioned activity lifecycle pattern and take the needed steps to insure your variables exist as you desire based on state.
the onResume() method will let you reset the variables when the program resumes, but will also do it when you return to the activity unless you add the logic that says you are coming from in the app, not the home page. Maybe onRestart() is what you really need? I'm not positive, but it's possible with onResume.
You app is probably not closing but remaining in background. Check advanced task manager and see if the app is running or not.
Uninitialized variables are bad. Don't do it. ALWAYS manually reset variables before using them for the first time.
Override the onPause, onResume, and onDestroy methods. onPause should save anything upon pausing, onResume should reload these values when it is resumed, and onDestroy will be called when your app closes. You can clean up stuff in onDestroy. See this link.