Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I recently launched my first iPhone app and it seems to have people in the Android community asking for it ... so I started developing w/ the SDK.

The first thing I noticed is that in my iPhone app I would store certain session wide variables in the appDelegate. As I don't have this structure in Android I'm curious how Android developers keep track of application state across the app (hopefully w/out a ton of singleton objects?)

If the singleton object approach is how most developers do this - how can I ensure the application starts at a clean state each time the user clicks the "home" button and re-clicks icon (is there something I can add to my manifest to ensure it doesn't support multitasking in this way?)

My app has a lot of session specific state and for the first iteration won't yet support multitasking :(

like image 692
JimmyBond Avatar asked May 06 '11 18:05

JimmyBond


People also ask

What is Android application state?

This is an approach created a BaseActivity that will be inherited by all Activities of your App, and in the BaseActivity perform saved. This means every time when an Activity is saving instance state, the Application's level state is also saved.

Can Android Studio app run on iOS?

In Android Studio, click File | New | New Module. In the list of templates, select Kotlin Multiplatform Shared Module, enter the module name shared , and select the Regular framework in the list of iOS framework distribution options. This is required for connecting the shared module to the iOS application.

Which file in Android manages the app's configuration?

EMM partners can read your app's configurations by using Google Play APIs. Create a file named app_restrictions. xml in your app's res/xml directory. The structure of that file is described in the reference for RestrictionsManager .


1 Answers

First, android app can consist of multiple Activities.

If you want to share state between Activities use Application class: How to declare global variables in Android?

If you only have one Activity, then you can save state there.

Beware: when activity is not Active (it's GUI not showing) it does not mean that it is killed. So if you use your app and then close it, open another app, then go back to you app, it might be still "alive" (kept in memory) and the state would be preserved.

The best way to handle this is to hook into Activity lifecycle. Then you can set/reset data at will.

like image 138
Peter Knego Avatar answered Sep 28 '22 05:09

Peter Knego