views:

119

answers:

2

Hi all, I am relatively new to the Android world and am having some difficultly understanding how the whole screen orientation cycle works. I understand that when the orientation changes from portrait to landscape or vice versa the activity is destroyed and then re-created. Thus all the code in the onCreate function will run again. So here's my situation: I have an app that I am working on where it logs into a website, retrieves data, and displays it to the user. While this is all done in background threads, the code that starts these threads is in the onCreate function. Now, the problem lies in that whenever the user changes the screen orientation, the app will log in, retrieve the data, and display it to the user again. What I would like to do is set a boolean that tells the app if it is logged in or not so it knows whether or not it must log in when the onCreate function is called. So long as the app is in memory the HttpClient will exist and contain the cookies from logging the user in but when the app is killed by the system those will go away. So I would assume that I need to do something like setting the logged in boolean to false when the app is killed but since onDestroy is called when the screen is rotated how is this possible? I also looked into the finalize function and isFinishing() but those seem to not be working.

Shorter version: How can I distinguish between when an app is being killed from memory from when an activity is being rotated and different code for each event?

Any help or a point in the right direction is greatly appreciated. Thank you!

+6  A: 

Matt, you need to tell the platform that your activity can handle orientation changes so that there's no need to restart it.
To do this, add android:configChanges="keyboardHidden|orientation" to activity declaration in AndroidManifest.xml. More info here.

alex
really helpful tip thanks!
jeremynealbrown
+2  A: 

As alex pointed out you can tell android to not restart your activity when doing orientation changes... When you have to restart your application (maybe screen orientation changes your render context etc...) you can get informed of the changes by overriding onConfigurationChanged(..) in your Activity.

Soulreaper