private boolean isLastActivity() {
final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> tasksInfo = am.getRunningTasks(1024);
final String ourAppPackageName = getPackageName();
RunningTaskInfo taskInfo;
final int size = tasksInfo.size();
for (int i = 0; i < size; i++) {
taskInfo = tasksInfo.get(i);
if (ourAppPackageName.equals(taskInfo.baseActivity.getPackageName())) {
return taskInfo.numActivities == 1;
}
}
return false;
}
You wil also require below permission in manifest....
<uses-permission android:name="android.permission.GET_TASKS" />
Thus in your Activty you can just use the following:
public void onBackPressed() {
if (isLastActivity()) {
showDialog(DIALOG_EXIT_CONFIRMATION_ID);
} else {
super.onBackPressed(); // this will actually finish the Activity
}
}
No comments:
Post a Comment