1

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?”

Community
  • 1
  • 1
anonim
  • 2,494
  • 6
  • 30
  • 40
  • 2
    "Regarding the fact that calling finish() in an activity’s onCreate(), onRestart() or onResume() is ignored by Android (according to the documentation) ." -- I have no idea where you read that, and it is certainly not the case. – CommonsWare Aug 17 '12 at 14:59
  • I'm sorry. It is really the case IMHO. referring to http://developer.android.com/reference/android/app/Activity.html, If you carefully pay attention to the activity lifecycle, an activity is not killable in methods like onCreate(), onRestart(), onStart(), .... please look at the table there. of course, although it says that it is not possible to kill an activity in onCreate(), it shamely does. – anonim Aug 17 '12 at 15:54
  • 1
    @anonim.developer: I think you are mis-interpreting what "killable" means in that context. Going by the paragraph immediately following the table I believe it simply means "not killable by the system" as opposed to " not killable by you the developer of the app via finish()". Meaning that you can rely on your code being executed in its entirety within those "unkillable" methods. – Nick Aug 17 '12 at 15:59

2 Answers2

2

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?

Nick
  • 8,181
  • 4
  • 38
  • 63
  • Thanks! and what I should do with currently opened activities if the user simply hit back on redirected sign in page? Sorry! the answer is not such simple as you think. – anonim Aug 17 '12 at 16:03
  • 1
    Well if you are going to leave that activity in the stack then it's on Resume() method will be called again and the user will just be redirected back to the login page. If you would rather have the application exit when they hit back, then you need to remove that activity from the stack before you redirect. It's your workflow not mine and I have no way of knowing what your desired result of pressing back, home, etc. would be. – Nick Aug 17 '12 at 16:07
  • So you want to put that check into every single Activities onResume method. Sounds like a nightmare. This is not a good solution. – lostintranslation Feb 01 '17 at 21:35
  • Why would you assume that? Where you put it depends on the specifics of the app. You can listen to activity lifecycle events in the application context or you can create an abstract base class for your activities if you want to do it in one place. – Nick Feb 01 '17 at 21:41
0

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.

Community
  • 1
  • 1
shi
  • 131
  • 3
  • as I replied to CommonsWare, it is not possible to close an activity in certain state. please refer to docs at developer.android.com/reference/android/app/Activity.html, – anonim Aug 17 '12 at 16:06
  • As I replied to your reply to CommonsWare, you are misinterpreting those docs. Read the paragraph immediately following the table which you are citing; you can in fact call finish from within onCreate() etc. If you doubt this assertion, it's easy enough to verify by trying. – Nick Aug 17 '12 at 16:39
  • 1
    Have you? It's your question so you should have. There's no reason to get hostile here, especially towards the people trying to help. The answer is yes it does work, however off the top of my head I can't think of a legitimate reason to call finish() from within onResume(), or at least I cannot think of a design that does such a thing that could not be improved and in the process avoid the call. – Nick Aug 17 '12 at 19:09