Android ActivityManager get top Activity

Description

Android ActivityManager get top Activity



import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;

import android.content.Context;

import java.util.List;

public class Main {
    public static void main(String[] argv) throws Exception {
    }/*from ww w . j a v  a  2s.c om*/

    public static boolean isTopActivity(Context context, String activityClassName) {
        List<RunningTaskInfo> tasksInfo = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE))
                .getRunningTasks(1);
        if (tasksInfo.size() > 0) {
            if (activityClassName.equals(tasksInfo.get(0).topActivity.getClassName())) {
                return true;
            }
        }
        return false;
    }

    public static boolean isTopActivity(Context context) {
        String activityName = context.getClass().getName();
        return isTopActivity(context, activityName);
    }
}



PreviousNext

Related