简体   繁体   中英

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 (ie 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 (ie how to log launches of an app) - but none that I have seen has yet been able to solve my problem. All ideas welcome...

What about

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

? This uses a simple Intent flag .

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

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

eg put the following into onCreate:

Class<?> myclass;

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

startActivity(new Intent(this, myclass));
finish();

尝试查看具有精心设计的解决方案的“ OI Safe”应用程序(我也不知道代码是否也经过精心设计,但是,您会发现:p)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM