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.

Paulo Barros
  • 2,740
  • 8
  • 28
  • 36
  • 1
    Can [that](http://stackoverflow.com/a/18115551/4762282) help you? – Ircover May 18 '15 at 13:33
  • @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 =). – Paulo Barros May 21 '15 at 20:42

3 Answers3

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.

Paul Harris
  • 5,769
  • 1
  • 25
  • 41
  • 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: http://stackoverflow.com/questions/18115493/android-dynamicly-get-the-current-activity-in-foreground/18115551#18115551 – Paulo Barros May 20 '15 at 15:03
  • 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. – Paul Harris May 20 '15 at 19:14
  • Are you talking about the use of the Solo constructors Solo(getInstrumentation(), activity) and Solo(getInstrumentation())? Playing with those didn't work for me. – Paulo Barros May 21 '15 at 20:39
  • 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. – Paul Harris May 22 '15 at 14:17
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());
Community
  • 1
  • 1
Ircover
  • 2,406
  • 2
  • 22
  • 42
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;
}
Denis Voloshin
  • 768
  • 10
  • 32