androidandroid-activityback-stack

Is it possible to dump/inspect the back stack?


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?


Solution

  • 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.