How to manage application state in Android (for iPhone developer)

I recently launched my first iPhone app, and it seems like people in the Android community are asking about this ... so I started development with the SDK.

The first thing I noticed was that in my iPhone application I would save certain session variables in appDelegate. Since I don't have this structure in Android, I'm curious how Android developers track the state of the application through the application (hope you get a ton of single objects?)

If the single-user approach to the object is how most developers do it - how can I guarantee that the application starts in a clean state every time the user clicks the home button and clicks the icon again (is there something I can add to your manifest to ensure it doesn't support multitasking this way?)

My application has a lot of session-specific state, and for the first iteration, multitasking is not yet supported :(

+6
source share
3 answers

Firstly, an Android application may consist of several activities.

If you want to split the state between actions, use the Application class: How to declare global variables in Android?

If you have only one action, you can save the state there.

Beware: when the activity is inactive (its GUI is not displayed), this does not mean that it is killed. Therefore, if you use your application, and then close it, open another application, and then return to your application, it can be "live" (stored in memory), and the state will be saved.

The best way to handle this is to connect to the activity life cycle . Then you can set / reset the data as you like.

+5
source

I believe Singletons is the best way to keep the state of the application in Android applications. You can listen when the user leaves the application using the onPause() and onStop() methods of the current focused action and does whatever he wants with your data at that moment. It is not recommended to check the OS lifecycle management of your application (for example, try to kill your process by clicking Back). If you want the state of the application to be reset every time a user leaves the application (by pressing the "Home" button or interrupted by a phone call or notification or whatever you have), just put all your session data into yourself. When the user leaves, he will be destroyed and recreated when the user returns.

Obviously, I don’t know about your application, but as soon as you get acquainted with the life cycle of each screen (Activity) and the application, you can use callbacks to manage your state, but you consider it necessary.

http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

+1
source

If you want to close the application when the user presses the "home" key, you can call finish () on your onPause method.

See this link: Killing an Android application in pause mode

+1
source

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


All Articles