I'm using appium for interaction between my Android device and java code. And I faced with problem that on some kind of devices(including emulators) after pressing on Home button, appium return incorrect current activity (it returns previous activity which is currently must be minimized). I found that appium used dumpsys window windows with grabbing mFocusedApp value for getting current app. I read another answers about getting Android current activities, and mostly it recommend to use:
adb shell "dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'"
That was the source of the problem, because after pressing Home button mCurrentFocus and mFocusedApp linked to different activities. But I can't find any explanation the difference between these fields. And why appium uses only mFocusedApp for it?
The explanation of the difference between
mCurrentFocusandmFocusedAppstares right at you:$ dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp' mCurrentFocus=Window{X uX package/.activity} mFocusedApp=AppWindowToken{X token=Token{X ActivityRecord{X uX package/.activity tX}}}The
mCurrentFocusis aWindow(just aviewwhich may or may not have anActivityRecordassociated with it)The
mFocusedAppis anAppWindowToken(anappTokenwhich will always have anActivityRecord)So when input focus switches to a
viewwith anactivity- bothmCurrentFocusandmFocusedAppwould show the sameactivity. But sometimes focus switches to aviewwithout anactivity(like parts of SystemUI, etc) - thenmCurrentFocuswill be showing thatviewbutmFocusedAppwill still be showing theActivityRecordof theappwhich had the focus before the last switch.