Android - Detecting Application Launch from Home or History

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?

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.

how to check whether the activity was launched from notification or from the user click on home screen dashboard app icon in android

There already were several questions on this topic:

  • detecting application launch from
    home or history
  • Determine if app was launched from
    home screen?

As of the home screen part, as far as I know there is no way to detect that.

However, you can detect whether your activity is launched from the notification icon:

When setting up your notification, you put an extra into it's PendingIntent, say fromNotification: boolean.

In your main activity's onCreate method you check the intent's extras, and if (there are any and) the fromNotification is among them with the value true, than you know it has been invoked by clikcing on the notification icon.

Detect when app just started or opened from Recent Apps

When you return to your app through the recent-tasks list, there are three possibilities:

  1. It has been a long time, over 30 minutes, and your process is not running. In that case, Android just launches your app as if the user tapped on your home screen launcher icon. You are not given your saved instance state, as it is deemed to be stale.

  2. It has not been a long time, so Android wants the user to think that your app has been running since they left it. However, due to memory pressure, Android terminated your process while it was in the background. In this case, Android forks a new process for you, creates an instance of your activity, and passes to you your saved instance state Bundle in onCreate() and onRestoreInstanceState().

  3. Your process is already running. In that case, your activity is just brought back to the foreground. You do not need your instance state, as your activity instance is still running. Android has not touched your widgets, and so if your UI is not the way you want it to be, that is your own fault, because you did something (e.g., in onPause() or onStop()) that screwed up your UI. From your descriptions, I am interpreting it that you are testing this scenario, in which case onRestoreInstanceState() is not called, because it is not needed.

So, as long as you do not screw up your own UI, your app will work as you described it for Google Plus, so long as you handle the saved instance state for scenario #2 above.

On Android 5.0+, there are related scenarios tied into the PersistableBundle that you can use with variations of onSaveInstanceState() and kin. I have not played with the PersistableBundle much and so I do not have any particular advice related to it.

Detect When other Application opened or Launched

You cannot detect an App launch in Android. But you can get the list of currently open apps using this code and check if the app you're looking for is open or not:

ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();

for (int i = 0; i < runningAppProcessInfo.size(); i++) {
if(runningAppProcessInfo.get(i).processName.equals("com.the.app.you.are.looking.for") {
// Do you stuff
}
}

You can also check if the app is running in the foreground using this method

public static boolean isForeground(Context ctx, String myPackage){
ActivityManager manager = (ActivityManager) ctx.getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > runningTaskInfo = manager.getRunningTasks(1);

ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
if(componentInfo.getPackageName().equals(myPackage)) {
return true;
}
return false;
}

How can one detect an Android application launching?

The application start Intent is not a broadcast, so there is no way to register a broadcast receiver and receive it. As previously answered here, there really is no way to detect the launch of the app. You could possibly write a service that polled the running tasks looking for the application's task (using the ActivityManager interface), but that's the best I can think of and it probably wouldn't be very performant.



Related Topics



Leave a reply



Submit