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
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.
What about
if((getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY )!=0) {
Log.d(TAG, "Called from history");
}
? This uses a simple Intent flag.
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)
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();