Debug the Android back stack in eclipse

2019-07-19 06:01发布

问题:

Is there a way to visualize the backstack with activities and fragements in Android in the Eclipse ADT IDE?

回答1:

Do you mean just see what it looks like for debugging purposes? In that case, define

public static void displayBackStack(FragmentManager fm) {
    int count = fm.getBackStackEntryCount();
    Log.d("Backstack log", "There are " + count + " entries");
    for(int i = 0; i<count; i++) {
        // Display Backstack-entry data like
        String name = fm.getBackStackEntryAt(i).getName();
        Log.d("Backstack log", "entry " + i + ": " + name);
    }
}

in some class C and call

C.displayBackStack(getFragmentManager());

or

C.displayBackStack(getSupportFragmentManager()); // with compatibility package

from your activity. This puts the BackStack in your log.

Of course, you can vary the data that you display according to your needs.