get Top Activity - Android Activity

Android examples for Activity:Top Activity

Description

get Top Activity

Demo Code


//package com.java2s;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;

import java.util.List;

public class Main {

    public static String getTopActivity(final Context context) {
        try {/*from   www . j  a v a2  s .com*/
            ActivityManager am = (ActivityManager) context
                    .getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningTaskInfo> tasks = am
                    .getRunningTasks(1);
            if (!tasks.isEmpty()) {
                ComponentName topActivity = tasks.get(0).topActivity;
                return topActivity.getClassName();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }
}

Related Tutorials