get Current Top Activity Class - Android Activity

Android examples for Activity:Activity Status

Description

get Current Top Activity Class

Demo Code


//package com.java2s;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;

import android.content.Context;

public class Main {
    @SuppressWarnings("unchecked")
    public synchronized static Class<? extends Activity> getCurrentTopActivityClass(
            Context con) {//from  w ww  .j  a va  2 s.co m
        ActivityManager mActivityManager = (ActivityManager) con
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager
                .getRunningTasks(1);
        ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
        String className = ar.topActivity.getClassName();
        Class<? extends Activity> clazz = null;
        try {
            clazz = (Class<? extends Activity>) Class.forName(className);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return clazz;
    }
}

Related Tutorials