如何获取 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/