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

3

10

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.

Milldam answered 15/5, 2015 at 3:54 Comment(2)
Can that help you?Boulware
@Ircover, yes. I would say my question is a duplicate question. Should you post your comment as an answer for me to accept? I'm not sure how to proceed here, but you saved my lfie =).Milldam
B
1

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());
Boulware answered 22/5, 2015 at 7:33 Comment(0)
C
2

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.

Chord answered 19/5, 2015 at 14:28 Comment(4)
Hello, Paul. Thanks a lot for the help. Unfortunately, this method is returning the Activity that my ActivityInstrumentationTestCase2 should be testing, not the current activity on focus. I could solve my problem by following this: #18115993Milldam
There used to be a known issue where it would tel you wrong if you constructed the solo activity in a certain way, if you post that then I can tell you if its that.Chord
Are you talking about the use of the Solo constructors Solo(getInstrumentation(), activity) and Solo(getInstrumentation())? Playing with those didn't work for me.Milldam
I was yes, the second version is the one more likely to work, does your activity use a tab host or something? I understand you have a workaround but seems wrong that robotium is getting confused.Chord
B
1

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());
Boulware answered 22/5, 2015 at 7:33 Comment(0)
S
0

This one works for me, the minimum SDK level is 18

public static Activity getCurrentActivity(){
    try {
        Class activityThreadClass = Class.forName("android.app.ActivityThread");
        Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
        Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
        activitiesField.setAccessible(true);
        ArrayMap activities = (ArrayMap) activitiesField.get(activityThread);
        for (Object activityRecord : activities.values()) {
            Class activityRecordClass = activityRecord.getClass();
            Field pausedField = activityRecordClass.getDeclaredField("paused");
            pausedField.setAccessible(true);
            if (!pausedField.getBoolean(activityRecord)) {
                Field activityField = activityRecordClass.getDeclaredField("activity");
                activityField.setAccessible(true);
                Activity activity = (Activity) activityField.get(activityRecord);
                return activity;
            }
        }
    }catch (Exception e){
        logger.error(e.getMessage());
    }
    return null;
}
Spence answered 21/5, 2015 at 17:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.