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 w
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.
You need to familiarize yourself with the Activity Lifecycle.
You could leverage onResume()
to reset your variables; also note onDestory()
and onPause()
.
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.
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.
I like what @Alex and @Jack said. To add to that, also consider that you can call finish()
in your Activity
if you want to force it to close up and return to the last Activity. Going along with this, also consider the use of setResult(int)
(JavaDoc Here)
You can also set a flag on the Intent
when you call the Activity
you are questioning about. A flag like FLAG_ACTIVITY_NO_HISTORY could be helpful:
If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute.
List of Intent Flags
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.