package com.chanryma.fetchtopactivity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ActivityC extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_c);
    }

    public void onButtonClick(View view) {
        List<Activity> activities = getActivities();
        StringBuilder strBuilder = new StringBuilder();
        if (activities != null) {
            for (Activity activity : activities) {
                if (strBuilder.length() > 0) {
                    strBuilder.append("\n");
                }

                strBuilder.append(activity.getClass().getSimpleName());
            }
        }

        ((TextView) findViewById(R.id.tv_result)).setText(strBuilder.toString());
    }

    public static List<Activity> getActivities() {
        List<Activity> activities = new ArrayList<Activity>();
        try {
            Class activityThreadClass = Class.forName("android.app.ActivityThread");
            Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
            Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
            activitiesField.setAccessible(true);

            Map<Object, Object> activityRecords = (Map<Object, Object>) activitiesField.get(activityThread);
            if (activityRecords == null) {
                return activities;
            }

            for (Object activityRecord : activityRecords.values()) {
                Class activityRecordClass = activityRecord.getClass();
                Field activityField = activityRecordClass.getDeclaredField("activity");
                activityField.setAccessible(true);
                Activity activity = (Activity) activityField.get(activityRecord);
                activities.add(activity);
            }
        } catch (Exception e) {
            Log.e("ActivityC", "", e);
        }

        return activities;
    }


    public static Activity getActivity() throws Exception {
        Class activityThreadClass = Class.forName("android.app.ActivityThread");
        Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
        Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
        activitiesField.setAccessible(true);

        Map<Object, Object> activities = (Map<Object, Object>) activitiesField.get(activityThread);
        if (activities == null) return null;

        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;
            }
        }

        return null;
    }
}

@@ -580,24 +580,233 @@
 tivities();%0A
+        String result = concatenateActivityNames(activities);%0A        ((TextView) findViewById(R.id.tv_result)).setText(result);%0A    %7D%0A%0A    private String concatenateActivityNames(List%3CActivity%3E activities) %7B%0A
         Stri
@@ -1154,58 +1154,15 @@
     
-((TextView) findViewById(R.id.tv_result)).setText(
+return 
 strB
@@ -1178,17 +1178,16 @@
 String()
-)
 ;%0A    %7D%0A
@@ -1191,29 +1191,257 @@
  %7D%0A%0A    
-public static
+/**%0A     * The order of activities in the returned list is not constant.%0A     * As far as this demo project is concerned, I expect the result to be %5BActivityA,ActivityB,ActivityC%5D, but it varies.%0A     * @return activities%0A     */%0A    private
  List%3CAc
@@ -2555,16 +2555,21 @@
 ityC%22, %22
+Error
 %22, e);%0A 

Refactor code and add comment.