开发者

Why is OnCreate() being called when the app is "quit", this causes my initiliazation code to run multiple times

So this should be easy I am sure I am just missing something.

in my OnCreate() method, I do most of my games initilization (oh by the way I am making a game!)... in OnCreate I create a Panel, which is a class that extends view, and is responsible for all displaying. In the constructor of this panel I update a few linkedlist with objects to display, these objects are moveable by me in the panel (I can click an object, click a new spot, and the object is moved there) That all works fine!

Now if I hit the home button to leave my app, and come back, android remembers where I left my objects, and keeps them there! How kind of Android. However the problem occurs when I hit the back button to leave my application (Which I guess is mo开发者_如何学编程re like killing it, which means OnCreate() is called the next time my app starts. (I know OnCreate() CAN be called if I just hit the home button, but only when the phone needs some space and kills my app) Anyways, when OnCreate is called again, Android so kindly remembers where the objects I moved are, but it does something else, it recalls all my initalization code, so there is duplicate objects, at the original location (aka now there is double the objects).. How do i fix this? I tried this,

 if(savedInstanceState!=null) //see if android saved the state
       {
                 super.onCreate(savedInstanceState); //reload the state
           }
   else{
   super.onCreate(savedInstanceState); //I pass this savedinstancestate to OnCreate here even 
//though it is null,
//because I do not know what else to pass
//then i do the initilization crap all over again (because the savedinstancestate should have been 
//null and therefore it shouldnt display any saved data locations,
// but.... YET IT DOES!!!
}

Please help me with this! How do I either stop it from saving the state EVER and always let me load it how I want to or how do I make it so my init code is ONLY called when the game doesnt reload the state? Thank you!!


onCreate() is called every time your activity / dialog / service is created for the first time. the savedInstance parameter is not really something android saves - it's your job to do that. note that the creation may take place for a number of reasons, including when you rotate screen. you can flip the bits in your AndroidManifest.xml file, right where you define your activities.

another question is what you do when your activity goes out of focus, say, it's pushed to the background. if you look at the charts for Activity you will figure that your application does not normally get closed when you press home. Android is a multitasking environment so hiding an app does not necessarily mean it gets closed, but you can do it, for example by calling finish() in your onStop() method (or even in onPause(), if you want any dialog, including sms popup, to kill your app).

if you need to save configuration for your activity (which will later be passed as a savedInstanceState, you should override onRetainConfiguration() call - it's not Android's job to do this (and by default nothing is saved, so your configuration is null most of the time)

so basically, if you want your application to be written the way you expect it to work, you should do it like this:

  • use onRetainConfiguration() to save where your objects are
  • call finish() in onStop() method
  • look into savedInstanceState in your onCreate() method to restore your objects

that will fix all your problems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜