A, pan

In Android development, it’s not uncommon to take on projects and code that are completely new to you. A big problem after I took over was that I was not familiar with the project and code, so I could not quickly find the corresponding Activity and Fragment classes on the UI, which was commonly known as “catch pot”. In view of the multiple pot project, was troubled by this problem many times, decided to write a small tool to solve this problem.

Second, the train of thought

Since we are taking over the project, we can’t make too many changes to the original project. After considering several options, adopt the following:

1, the Application to monitor ActivityLifecycleCallbacks to monitor the Activity lifecycle, and join the stack. 2. Use notifications to avoid changes to the current UI. Generate a notification in the notification bar, and click on the notification to pop up a list of Actitivy for the current stack.

Three, implementation,

1. Listen for the Activity lifecycle

public class DebugStackHelper {
    / / the Activity stack
    private static Stack<Activity> stack = new Stack<>();

    public static void init(Application application, boolean debug) {
        application.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
                stack.add(activity);
            }
            @Override
            public void onActivityDestroyed(@NonNull Activity activity) { stack.remove(activity); }}); }}Copy the code

Generating the notification bar is relatively common. After clicking the notification bar, obtain the last Acitvity in the current stack, and display the content in the stack in the popup window in this Activity

    // Displays pop-ups
    private static void show(a) {
        Activity activity = stack.lastElement();
        Dialog dialog = new Dialog(activity);
        View view = activity.getLayoutInflater().inflate(R.layout.layout_debug_stack, null);
        TextView tvStack = view.findViewById(R.id.tv_stack);
        addView(tvStack);
        dialog.setContentView(view, new ViewGroup.LayoutParams(w, h));
        dialog.show();
    }
Copy the code
The list of stacks is displayed in the pop-up window

Check the stack for the name of the Fragment in the Activity and the name of the Fragment in the Activity (only check the Fragmnet in the second layer).

    private static void addView(TextView tvStack) {
        StringBuilder sb = new StringBuilder();
        for (int i = stack.size() - 1; i >= 0; i--) {
            Activity activity = stack.get(i);
            sb.append("\n");
            sb.append("◉");
            sb.append(activity.getClass().getSimpleName());
            if (activity instanceof FragmentActivity) {
                FragmentManager manager = ((FragmentActivity) activity).getSupportFragmentManager();
                List<Fragment> fragments = manager.getFragments();
                for (int i1 = 0; i1 < fragments.size(); i1++) {
                    Fragment fm = fragments.get(i1);
                    String simpleName = fm.getClass().getSimpleName();
                    if (!"SupportRequestManagerFragment".equals(simpleName)) {
                        sb.append("\n");
                        sb.append("*");
                        sb.append(simpleName);
                        List<Fragment> fragmentListChild = fm.getChildFragmentManager().getFragments();
                        if(fragmentListChild.size() ! =0) {
                            for (int j = 0; j < fragmentListChild.size(); j++) {
                                Fragment fmChild = fragmentListChild.get(j);
                                String nameChild = fmChild.getClass().getSimpleName();
                                if (!"SupportRequestManagerFragment".equals(nameChild)) {
                                    sb.append("\n");
                                    sb.append("*");
                                    sb.append(nameChild);
                                }
                            }
                        }
                    }
                }
            }
            sb.append("\n");
        }
        tvStack.setText(sb);
    }
Copy the code

Effect of four,

Five, the source

The code has been packaged as a utility class and uploaded to Github

DebugStackHelper