Skip to content

Navigating Between Views

Natan Vieira edited this page Sep 14, 2023 · 22 revisions

Navigating between views using InventoryFramework is pretty simple and straightforward, but there's some "little things" that you must understand about Data Transitivity and Shared Contexts before really diving into it.

Basic Usage

Let's get started with the most basic usage of navigation between views, we have the following views:

"A" view with a DIAMOND item on it that navigates to "B";

Screenshot 2023-09-13 at 21 21 44
// A.java
@Override
public void onInit(ViewConfigBuilder config) {
    config.title("A");
}

@Override
public void onFirstRender(RenderContext render) {
    // Moves player to "B" view on click
    render.firstSlot(new ItemStack(Material.DIAMOND))
        .onClick(click -> click.openForPlayer(B.class));
}

"B" view with a REDSTONE item on it that navigates back to "A";

Screenshot 2023-09-13 at 21 23 01
// B.java
@Override
public void onInit(ViewConfigBuilder config) {
    config.title("B");
}

@Override
public void onFirstRender(RenderContext render) {
    // Moves player to back to "A" view on click
    render.firstSlot(new ItemStack(Material.REDSTONE))
        .onClick(click -> click.openForPlayer(A.class));
}

And how it works? What we have here?

Take a look inside the onClick method of each item, we have a openForPlayer method, that method is responsible for opening a view to the player that clicked on the item. Its signature is similar to ViewFrame's open but without the view class parameter. Pretty simple.

openForPlayer vs. openForEveryone

It is possible to return to the previous screen using .back() the name of this is "Browsing History" as soon as a screen is opened from another this function will take the player back to the previous screen.

class A extends View {

    @Override
    public void onFirstRender(RenderContext render) {
        // Moves player to "B" view on click
        render.firstSlot(...).onClick(click -> click.open(B.class));
    }

}

class B extends View {

    @Override
    public void onFirstRender(RenderContext render) {
        // Moves player back to "A" view
        render.firstSlot(...).onClick(click -> click.back());
    }

}

Creating Flows with back() and onResume

Data Transitivity

Data transitivity during navigation between screens can simplify data resolution so that it is not necessary to re-declare each piece of data at each navigation.

In the "Opening and Closing" in Basic Usage or "Initial State" in Advanced State Management teaches you how to apply initial data when a view is opened, this data disappears when the view closes, however, you can keep it in case of transition from one screen to another.

Clone this wiki locally