Android How to - Get Top Activity Class Name With Package








Question

We would like to know how to get Top Activity Class Name With Package.

Answer

/*from w w w . java  2s .  co m*/
import java.util.List;

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

public class Main {

  public static String getTopActivityClassNameWithPackage(final Context context) {
     ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
          List<RunningTaskInfo> tasks = am.getRunningTasks(1);
          if (!tasks.isEmpty()) {
              ComponentName topActivity = tasks.get(0).topActivity;
              return topActivity.getClassName();
          }
          return null;
  }
  
}