mattlogan / Pancakes

A View-based navigation stack for Android

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add StackChangedListener

mattlogan opened this issue · comments

This is in response to the previously closed pull request #1.

I think it is useful to have a StackChangedListener so consumers can be notified of changes to the navigation stack. This may useful for situations like updating the host Activity's Toolbar with data provided by a View. For example:

public class DogsView implements NamedView {
    // ...

    @Override
    public String viewName() {
        return "Dogs";
    }
}

Then in the host Activity:

public class DogActivity extends AppCompatActivity {
    // ...

    @Override
    public void onStackChanged() {
        NamedView topView = (NamedView) viewStack.peekView();
        toolbar.setTitle(topView.viewName());
    }
}

Added commit 2a1d677

The tricky part is knowing when to notify the listeners, especially in the case of an animation. I settled on notifying after BOTH the ViewStack's size AND the top View in the ViewGroup container have changed. For a push with an animation, this happens before the animation starts (right after the new View is added to the container). For a pop with an animation, this happens after the animation completes (right after the old view is removed from the container).

Feedback is welcome. I'll think about this a bit more before writing some tests and preparing a release.

Thanks for considering adding the listener. What you posted above is basically what I ended up doing in Volume Butler. I just converted most of the app over to views using Pancakes. Seems to be pretty solid way to make sure things display correctly.

Awesome! Yeah, I'm not sure I'd use the listener in my own app. I think I prefer calling stuff on the Activity from the Views in onFinishInflate(). But it's a valid use case nonetheless, and I think it's worth supporting!