Returns Class name of base activity - Android Activity

Android examples for Activity:Activity Feature

Description

Returns Class name of base activity

Demo Code


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

import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;

public class Main {
    /**/*from  ww w  . j av a  2s. com*/
     * Returns Class name of base activity
     * @param context Context to provide activity information
     * @return String representing class name of base activity
     */
    public static String getBaseActivityClassName(Context context) {
        ComponentName activity = getBaseActivity(context);
        if (activity == null) {
            return null;
        }
        return activity.getClassName();
    }

    private static ComponentName getBaseActivity(Context context) {
        ActivityManager am = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        if (am == null) {
            return null;
        }
        List<RunningTaskInfo> info = am.getRunningTasks(1);
        if (info == null) {
            return null;
        }
        ComponentName activity = info.get(0).baseActivity;
        return activity;
    }
}

Related Tutorials