android determines whether an Activity is at the top of the stack

  • 2021-08-31 08:59:34
  • OfStack

In actual development, we need a lot of situations to judge whether an activity is at the top of the stack, which may bring troubles to new partners, so go directly to the code, and there are only a few lines


/** 
 * 
 *  Judge sth activity Is it at the top of the stack  
 * @return true At the top of the stack  false Not at the top of the stack  
 */ 
private boolean isActivityTop(Class cls,Context context){ 
 ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 
 String name = manager.getRunningTasks(1).get(0).topActivity.getClassName(); 
 return name.equals(cls.getName()); 
} 

Related articles: