Codz
Codz

Reputation: 315

Android - detecting application launch from home or history

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...

Upvotes: 16

Views: 17311

Answers (4)

bk138
bk138

Reputation: 3088

What about

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

? This uses a simple Intent flag.

Upvotes: 18

Christopher Masser
Christopher Masser

Reputation: 829

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();

Upvotes: 2

16aR
16aR

Reputation: 1

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)

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007554

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.

Upvotes: 15

Related Questions