Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out which Activity is on top of the stack using Robotium/Android SDK?

I have a Robotium test for an Android application, which extends ActivityInstrumentationTestCase2. The test operates on a loop, randomly clicking on active views. I would like to verify at the start of each iteration which Activity is currently focused. This behavior is important for me because one of the buttons is capable of starting another Activity, making further actions in the loop not possible, since they refer to the Activity under test (this is when I stop the Robotium test).

I would like a generic solution that would work for any Activity, without the need to change the onDestroy() method. This solution must also work for when the Home button is pressed.

like image 638
Paulo Barros Avatar asked May 15 '15 03:05

Paulo Barros


2 Answers

You should be able to use

solo.getCurrentActivity()

for this purpose, is this not working for you? If not ill pre-empt the potential issue and ask you for your code when you construct the solo object and which version of roborium you are using.

like image 160
Paul Harris Avatar answered Dec 07 '22 17:12

Paul Harris


As we found out, this link contains the answer to this question.

ActivityManager am = (ActivityManager) this .getSystemService(ACTIVITY_SERVICE);
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
Log.d(WebServiceHelper.TAG, "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName()+"   Package Name :  "+componentInfo.getPackageName());
like image 26
Ircover Avatar answered Dec 07 '22 18:12

Ircover