here's the question: MainActivity is a starting screen to launch activities A, B, C and so on. Before activity A is launched, it has to make some checks that take time, so a fast user can open activity B before activity A gets to the screen. If there's anything on top of the activity stack above MainActivity, activity A should not be opened to avoid inconsistent state. So I found a way to make it work for API >= 23, but our min is 21. Before API 23, taskInfo.topActivity is not available and I can't understand how to achieve this behavior. Here's my solution for API >= 23:
ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Activity.ACTIVITY_SERVICE);
List<ActivityManager.AppTask> appTasks = activityManager.getAppTasks();
ActivityManager.RecentTaskInfo taskInfo = appTasks.get(0).getTaskInfo();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
ComponentName topActivity = taskInfo.topActivity;
if (topActivity.getClassName().contains(MainActivity.class.getSimpleName())) {
// start activity
}
} else {
}
I would really appreciate your help for the else case)