16

What is the best way to detect when an Android "Application" has been launched from the Home screen/History screen?

Basically, what I'm trying to achieve is force the user to login to certain screens each time they come back to the app (i.e. they have full access to all activities once logged in, but essentially I want them to re-authenticate when they come back to the app via launching on the home screen).

I know similar questions have been asked before (i.e. how to log launches of an app) - but none that I have seen has yet been able to solve my problem. All ideas welcome...

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Codz
  • 315
  • 1
  • 2
  • 10

4 Answers4

18

What about

    if((getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY )!=0) {
        Log.d(TAG, "Called from history");
    }

? This uses a simple Intent flag.

bk138
  • 3,033
  • 1
  • 34
  • 28
  • Could someone comment on this answer's validity? Does it make sense to other, more experienced Android developers than myself? – marienke Nov 29 '13 at 12:23
  • 1
    You can simply test it out - works on all our devices here. If not, please inform me. – bk138 Dec 03 '13 at 09:55
  • NB that this deals with app launches, not getting a running app back from background. – bk138 Mar 05 '15 at 21:12
  • 2
    FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY flag is set when the activity is getting recreated after launch from recent or history but if activity only gets resumed FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY will not be set. above code will return false http://stackoverflow.com/questions/24065146/flag-activity-launched-from-history-not-set-in-nexus-10-android-4-4-2 – all-ok Jun 15 '16 at 13:14
15

What is the best way to detect when an Android "Application" has been launched from the Home screen/History screen?

You can't, AFAIK.

Basically, what I'm trying to achieve is force the user to login to certain screens each time they come back to the app (i.e. they have full access to all activities once logged in, but essentially I want them to re-authenticate when they come back to the app via launching on the home screen).

Please use a sensible, user-friendly login system. For example, if you feel that their login credentials are stale based upon time, then force them to log in again. You can do this by checking the credentials in onCreate(), and if they are stale, call startActivity() to launch your login activity (or pop up your login dialog, or whatever is your means of logging them in).

Of course, an even better approach is to skip the login entirely. Unless this is a "password safe", a banking app, or something else that needs higher-than-average security, you do not need them to log in, and your users will get irritated if they feel that your login requirement is unnecessary. Most mobile applications do not require authentication.

Forcing a login based upon how they reached the activity is user-hostile. You are telling users that deign to use their phones for things other than your app that they are second-class citizens.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Thanks for your response - unfortunately this is for a client, which requires a fairly high level of security (data is quite sensitive). I didn't think there was a way from my searches, and a time based session was exactly where I was heading. – Codz Mar 27 '11 at 18:51
  • 2
    And yet again clients ruin the day with a splash >_<, will need to find a workaround. – Warpzit Jun 05 '14 at 09:36
  • you may close the session when the activity pauses, turn on a flag to close the activity, close the activity on pause. – pojomx Feb 27 '15 at 16:35
  • 2
    I also fear clients won't take CommonWare's answer as an option.@Warpzit – Josh Mar 31 '15 at 12:35
2

simply create a stump activity that doesn't have a content view and launches other activities on application start

e.g. put the following into onCreate:

Class<?> myclass;

if(isTimeForActivity1){
    myclass = Activity1.class;
}else if(isTimeForActivity2){
    myclass = Activity2.class;
}

startActivity(new Intent(this, myclass));
finish();
Christopher Masser
  • 809
  • 13
  • 25
0

Try to look at the "OI Safe" application which has a well designed solution (I don't know if the code is well designed too, but, you'll look :p)

16aR
  • 1