androidandroid-activitylifecycle

How to check if an activity is the last one in the activity stack for an application?


I want to know if user would return to the home screen if he exit the current activity.


Solution

  • UPDATE (Jul 2015):

    Since getRunningTasks() get deprecated, from API 21 it's better to follow raukodraug answer or Ed Burnette one (I would prefer second one).


    There's possibility to check current tasks and their stack using ActivityManager.

    So, to determine if an activity is the last one:

    Please note, that above code will be valid only if You have single task. If there's possibility that number of tasks will exist for Your application - You'll need to check other taskList elements. Read more about tasks Tasks and Back Stack