1

For debugging purposes I'd like to be able to log the activities in the back stack, but I've not seen any API to do that nor can I see it mentioned while searching. Is there a way to do so?

Gruntcakes
  • 37,738
  • 44
  • 184
  • 378

3 Answers3

2

Put this code in onCreate of your MainActivity

getSupportFragmentManager().addOnBackStackChangedListener(
            new FragmentManager.OnBackStackChangedListener() {
                public void onBackStackChanged() {
                    for (int i = 0; i < getSupportFragmentManager().getBackStackEntryCount(); i++)
                        System.out.println(getSupportFragmentManager().getBackStackEntryAt(i).getName());
                }
            });

The name printed is defined when you call addToBackStack:

...    
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
...
ft.addToBackStack(name);
...

You can see the output in logcat.

1

Actually, i really stack with this question too. Unfortunatelly no. And let's think why?

Java get to our hands :

1) Annotations - way with DI.(Reflection way) like a ButterKnife
2) Parent class for log in background.

public class MainActivity extends LogActivity //for ex

3) With Thread.currentThread().getStackTrace() but you should check where you stay at the moment, what the thread you inspecting now and other routine work.

Referal links here, here and here.

Community
  • 1
  • 1
Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119
1

You can use the following command adb shell dumpsys activity It will print a lot of stuff including the back stack at the beginning:

Running activities (most recent first):
  TaskRecord{45168b4 #2194 A=com.example.myapp U=0 StackId=22 sz=3}
    Run #2: ActivityRecord{a05a1af u0 com.example.myapp/com.example.myapp.activity.MyActivity t2194}
    Run #1: ActivityRecord{5c017cd u0 com.example.myapp/com.example.myapp.activity.MySecondActivity t2194}
    Run #0: ActivityRecord{6fe38b2 u0 com.example.myapp/com.example.myapp.activity.MyThirdActivity t2194}

Another solution is to first type adb shell and press enter, followed with dumpsys activity | grep -i run. This will only show the back stack and no other stuff, but it takes two commands.

Wirling
  • 4,810
  • 3
  • 48
  • 78