variables retaining values after app close

2019-03-06 08:46发布

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.

6条回答
迷人小祖宗
2楼-- · 2019-03-06 08:56

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

查看更多
闹够了就滚
3楼-- · 2019-03-06 09:02

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.

查看更多
4楼-- · 2019-03-06 09:06

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.

查看更多
贼婆χ
5楼-- · 2019-03-06 09:11

You app is probably not closing but remaining in background. Check advanced task manager and see if the app is running or not.

查看更多
迷人小祖宗
6楼-- · 2019-03-06 09:13

Uninitialized variables are bad. Don't do it. ALWAYS manually reset variables before using them for the first time.

查看更多
beautiful°
7楼-- · 2019-03-06 09:15

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.

查看更多
登录 后发表回答