Weird behaviour when trying to retain selection in select_box


I am developing a multi-page app and my goal is to retain various user options when going back and forth between pages. Let me give an example from a selectbox (the same thing happens when using radio buttons)

values = ['val_1', 'val_2']

if 'example_index' not in st.session_state:
    st.session_state.example_index = 0

st.session_state.example_index = st.selectbox(
    'Select one value: ',
    format_func=lambda x: values[x],

When I try to select the second option the page quickly refreshes (expected behavior) but the selected item is still the same. Only when I try for a second time, the selection actually works. The same thing happens every time I try to jump between the two options.

I would really appreciate it if someone can explain what really happens in this example.