1

I have an app that has a few screens. The Main screen automatically opens a "new" screen if it's the first time the user opens the app. I then set a boolean variable (on the Main screen) keeping track of this.

The intention is if the user goes back to the Main screen, the code that opens the "new" screen can be skipped.

The problem is that the variable keeps getting reset on OnCreate. So, I added some code to use SharedPreferences.

This works; however, I want to clear the variable when the app exits. (I want the "new" screen to open every time the app opens the first time). So, looking at the lifecycle I tried both onStop and onDestory. The SharedPreferences are cleared but... not when the app is exited; but when the "new" screen appears.

Am I looking at the lifecycle wrong? Is there some sort of global variable I can declare that only lives while the app is open?

This functionality is the requirement, so I cannot change it.

kylemac
  • 189
  • 4
  • 19
  • 1
    All I can think is a seperate class to keep the variable and reset it when the app closes. Logging the time would work 100% too. But I guess I have a lot to learn because I have recently started android development – ntakouris Nov 11 '14 at 20:52
  • U can only know when an activity is closed by the user (but not when killed by a process manager). – eduyayo Nov 11 '14 at 20:54
  • Post some code and your manifest please. There should be an easy way to solve this problem. – David Wasser Nov 12 '14 at 22:04

2 Answers2

1

You should use onSaveInstanceState and onRestoreInstanceState, they will keep the boolean alive if your activity calls onCreate but not if you exit and come back later.

See this answer for implementation: Saving Android Activity state using Save Instance State

Community
  • 1
  • 1
Lukos
  • 712
  • 4
  • 5
  • I initially saw that link, and tried that. However, I couldn't get the onRestoreInstanceState to fire. So, then later in that post, I saw the SharedPreferences. I'm going back and looking at it, but still can't get the event to fire. So I'm looking into this: http://stackoverflow.com/questions/5574462/why-onrestoreinstancestate-never-gets-called – kylemac Nov 11 '14 at 21:09
  • Are you saving the boolean in onPause()? That is the only safe place to do the saving. EDIT: Also, you can force onRestoreInstanceState() by calling it in your onCreate. EDIT: And don't forget to call super.OnSaveInstanceState() in the save method. – Lukos Nov 11 '14 at 21:11
  • I still can't get it to fire. What code would you use on the onPause() to access the savedInstanceState bundle to save the variable? Everything I'm looking at doesn't give examples of that. – kylemac Nov 12 '14 at 03:20
  • Sorry, saving in onPause actually saves longer then when you quit the activity. You can only do the instance states, can you post your code with oncreate and the saved and restored overrides? – Lukos Nov 12 '14 at 04:00
  • Lukos, I was asking what code as in a code (sample) example for accessing the savedInstanceState bundle in the onPause method. Thanks. – kylemac Nov 17 '14 at 21:08
-1

You can define the variable in the line one of ur whole code, that way it will only reset when the app is opened again.

GjdGr8
  • 133
  • 1
  • 1
  • 7