anonim
anonim

Reputation: 2544

Android Whole Application State Management

I’m developing application for most major Mobile OS like iOS, Windows Phone and Android. I have a request from my client that simply possible to implement in iOS and WP but sounds really tricky in Android.

In iOS and WP, an application lifecycle is controlled through events which an object like UIApplication and Application receives.

In iOS, for ex., applicationDidEnterBackground:, applicationWillEnterForeground:, applicationWillTerminate: and the like, clearly define application states such as Inactive, Active and Background and make the app state management logic really straight forward.

In WP, Application receives well understanding events such as Launching, Deactivated, Activated, and Closing which make it really simple to decide what should be done in each app state logically to save as restore application wide object model.

But in Android, application state management sounds really difficult with Activities’ state changes such as onCreate, onRestartonDestroy method overriding. My problem arises where I want to control the whole application state when the user session goes expired and I want to redirect user to the sign in activity and shuts down other open activity.

Regarding the fact that calling finish() in an activity’s onCreate(), onRestart() or onResume() is ignored by Android (according to the documentation) .

Even if I override android.app.Application and put the logic there, it sounds like controlling open activities is not possible.

I almost tried all possible combinations of activity launch mode (such as SingleTask and SingleInstance) though I cannot produce behavior like those exist in iOS and WP.

There is another post related to this question which may clarify my problem more.

The question exactly is, “Is it possible to produce iOS or WP application behavior in Android anyway?”

Upvotes: 1

Views: 2874

Answers (2)

shi
shi

Reputation: 131

There's an answer here about the application state that may interest you:

Checking if an Android application is running in the background

With Application, you get the onCreate and you can put some logic here. So yeah, it's not as straight forward as in iOS but it's doable.

If it's just a session state, create a base activity that check against the session state and inherit all your activities from it.

You can close all your activties by using the Android SDK before going to the login page or... lock the back button.

Upvotes: 0

Nick
Nick

Reputation: 8317

So essentially, once a "session" expires, no matter what the user tries to do, you want them to be redirected to a login activity, yes?

Assuming you have a method you can call which tells you whether or not a session has expired, why no simply check that method in onResume() etc. and if the session has expired, redirect the user to the login Activity?

Upvotes: 2

Related Questions