Android Determine when the application will be completed or destroyed to change the orientation of the screen.

I'm relatively new to the Android world, and it's hard for me to understand how the entire screen orientation cycle works. I understand that when the orientation changes from portrait to landscape or vice versa, the action is destroyed and then recreated. Thus, all the code in the onCreate function will run again. So, here is my situation: I have an application in which I work on where it is registered on the website, extracts the data and displays it to the user. Although this is all done in the background thread, the code that starts these threads is in the onCreate function. Now the problem is that whenever the user changes the orientation of the screen, the application will register, retrieve the data and display it again to the user. What I would like to do is set a boolean to tell the application if it is registered or not, so it knows if it should register when the onCreate function is called. While the application is in memory, the HttpClient will exist and contain cookies from the user's login, but when the application is killed by the system, they will disappear. So I would suggest that I need to do something like setting logged in boolean to false when the application is killed, but since onDestroy is called when the screen is rotated, how is this possible? I also looked at finalize and isFinishing (), but they don't seem to work.

A shorter version: how can I distinguish when an application is killed from memory from the moment of the turn of activity and different code for each event?

Any help or point in the right direction is welcome. Thanks!

+4
source share
2 answers

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

+9
source

As alex pointed out, you can say that android does not restart your activity when the orientation changes ... When you need to restart the application (maybe the orientation of the screen will change your visualization context, etc.), you can get information about the changes by overriding onConfigurationChanged (..) in your activity.

+2
source

Source: https://habr.com/ru/post/1304634/


All Articles