user2676468
user2676468

Reputation:

Activity has a method called getTaskId(). How do I know if this task id matches the task id of the Application task stack?

I can get the app ID from my running activity via activity.getTaskId(). It will report back 185. If I go to another app, and start my activity from a share button, it will be placed IN THAT apps stack. If I do activity.getTaskId() it will report back 192 for example. I am assuming that an application process can only have one main task stack associated with it. How do I get that tasks ID? I want to be able to know "Hey I'm running outside of your apps task stack".

I contemplated doing this by polling the taskId the first time my activity is created and set that as a member variable to my Application Class, but if my app is killed, and then started first from another application, it will have the incorrect task id as the "AppTaskStackId". I haven't found any API for this.

Upvotes: 5

Views: 9886

Answers (3)

Leszek
Leszek

Reputation: 1231

A recently (API 29) added TaskInfo:

https://developer.android.com/reference/android/app/TaskInfo

should help here.

Upvotes: 0

JesusFreke
JesusFreke

Reputation: 20292

A different approach might be to have both an exported and non-exported activity. The exported activity would simply be forwarded on to the non-exported activity, but with an extra denoting that it was started externally. And then when starting the activity internally, you always call the non-exported activity without that "isExternal" extra.

And then, in the non-exported activity, you can check for the existence of that extra to determine if the activity was started internally or externally.

Upvotes: 2

Jeshurun
Jeshurun

Reputation: 23186

The only way I could find to get even close to what you are trying to accomplish would be with the following code in, say, your Activity's onCreate:

ActivityManager m = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTaskInfoList =  m.getRunningTasks(1);
if(!runningTaskInfoList.isEmpty()) {
    String callingPackageName = runningTaskInfoList.get(0).baseActivity.getPackageName();
}

Here callingPackageName would be set to the package name of your app if the activity has been invoked from another activity in your own app, or is the main activity of your app.

However, if the activity was started by another app, say using the share function, then callingPackageName would be the package name of the calling app, so all you have to do is check if this is equal to your app's package name by calling getPackageName().

Also note that:

  • Your app will now need the android.permission.GET_TASKS permission
  • The documentation for this method states:

Note: this method is only intended for debugging and presenting task management user interfaces. This should never be used for core logic in an application, such as deciding between different behaviors based on the information found here. Such uses are not supported, and will likely break in the future.

So I'm not sure how reliable this is or if it is even useful to you.

Upvotes: 1

Related Questions