get Top Activity Name - Android Activity

Android examples for Activity:Top Activity

Description

get Top Activity Name

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 getTopActivityName(Context context) {
        try {/*w  w  w.j  a v  a  2  s.  c  o m*/
            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