Android获得栈中最顶层的Activity
Posted Hello
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android获得栈中最顶层的Activity相关的知识,希望对你有一定的参考价值。
1 /** 2 * 获得栈中最顶层的Activity 3 * 4 * @param context 5 * @return 6 */ 7 public String getTopActivity(Context context) 8 { 9 android.app.ActivityManager manager = (android.app.ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE); 10 List<ActivityManager.RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1); 11 12 if (runningTaskInfos != null) 13 { 14 return (runningTaskInfos.get(0).topActivity.getClassName()); 15 } else 16 return null; 17 } 18 19 try 20 { 21 Intent intent = new Intent(MainActivity.this, Class.forName("com.johnson.main.Activity1")); 22 startActivity(intent); 23 } catch (ClassNotFoundException e) 24 { 25 e.printStackTrace(); 26 } 27 28 29 ================ 30 31 32 Context context = createPackageContext("com.johnson.main.common.Activity2",
Context.CONTEXT_INCLUDE_CODE |
Context.CONTEXT_IGNORE_SECURITY); 33 openDiaog((Activity) context); 35 Class clazz = context.getClassLoader().loadClass(topActivity);//载入这个类 36 Object owner = clazz.newInstance(); //新建一个实例 37 Object obj = clazz.getMethod("openDiaog", String.class).invoke(owner, context); //获取print方法,传入参数并执行
以上是关于Android获得栈中最顶层的Activity的主要内容,如果未能解决你的问题,请参考以下文章