android - Lollipop : Get top activity name

标签 android android-activity android-task

如何获取 Lollipop 中的顶级 Activity 名称?

((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE)).
   getRunningTasks(1).get(0).topActivity

不再适用于 Lollipop:

     * @deprecated As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method
     * is no longer available to third party
     * applications: the introduction of document-centric recents means
     * it can leak person information to the caller.  For backwards compatibility,
     * it will still retu rn a small subset of its data: at least the caller's
     * own tasks, and possibly some other tasks
     * such as home that are known to not be sensitive.

从 MyActivity 中的 onResume 调用

MyApplication.getInstance().saveCurentActivity(MyActivity.this)
saveCurentActivity(Activity a) {
    this.activityName = a.getClass().getSimpleName();
}

这不是一个好主意,因为 MyApplication 可能会因错误而被销毁(例如,NPE)。

最佳答案

这段代码对我有用。

ActivityManager activityManager = (ActivityManager)    context.getSystemService(Activity.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = activityManager.getRunningTasks(1);
String currentTopActivity = taskInfo.get(0).topActivity.getClassName();

它将提供 TopActivity。

关于android - Lollipop : Get top activity name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34355115/

相关文章:

java - 如何在运行时确定retrofit的版本?

java - 简单的 XML 序列化程序无法满足元素

java - 谷歌云消息开发者 Android 代码崩溃

android - Android中屏幕锁定和主页按钮之间的区别

android - 如何恢复现有任务的当前状态

android - 如何以编程方式在 android CalendarView 中突出显示多个日期

android - Android/iOS 上的 Base64 编码图像

java - 如何通过点击生成新 Activity

android - 是什么让 singleTask Activity 有 2 个实例?

Android Activity 错误地重新父级到应用程序任务