beamer icon indicating copy to clipboard operation
beamer copied to clipboard

App was closed when I pressed on back button

Open iEricKoh opened this issue 2 years ago • 4 comments

https://github.com/slovnicki/beamer/tree/master/examples/bottom_navigation_multiple_beamers

base on the example above, when I navigated from "books" -> "articles". at this moment, the app will be close when I pressed on back button on android while I am on "articles" screen.

is there a way that I can go back to the previous screen which is "books" screen when I hit the back button?

iEricKoh avatar Jun 14 '22 16:06 iEricKoh

Hey @iEricKoh :wave: Thanks for creating an issue and sorry for a late response.

This seems like a bug, indeed. A temporary solution can be to include a timestamp (or anything that will not stay the same) into the key for root BeamPage, something like this in the routerDelegate for MaterialApp.router:

locationBuilder: RoutesLocationBuilder(
      routes: {
        '*': (context, state, data) => BeamPage(
              key: ValueKey(
                '${state.routeInformation.location}-${DateTime.now()}',
              ),
              child: AppScreen(),
            ),
      },
    ),

The problem is because all the navigation is done inside inner Beamers and the key of this root BeamPage stays the same so it never rebuilds, not even when we do want to rebuild it, because what happens is:

  • AppScreen gets wrapped with BeamPage internally and Beamer creates a key from the initial path, that's /books
  • We navigate with bottom tabs to /articles, but handle that without doing anything on the root Beamer, i.e we don't rebuild the AppScreen with key containing /articles. Generally, this is what we want, not rebuilding everything because we would lose state of inner Beamers
  • Then we press the Android back button
    • it tries to pop, cannot
    • it tries to beamBack, can
    • it calls beamBack (which beams to /books) on the root router
    • AppScreen was already wrapped with BeamPage with key: ValueKey('/books') so the Navigator thinks it doesn't need to rebuild anything

I'll definitely try to solve this issue so we don't need the above workaround :thinking:

slovnicki avatar Jun 17 '22 23:06 slovnicki

@slovnicki Thanks for your solutions and the reason why it didn't works.

I've tried the solution above and it works fine!

tho there's still something wrong when I tried more complex routing between two different tabs.

Books -> Articles -> (press back) -> Books // works

Books -> Articles -> Single Article -> tap Books tab -> 
tap Articles tab(displaying single article) -> (press back) -> 
Articles -> (press back) -> app closed // it should go to books tab, isn't it?

iEricKoh avatar Jun 22 '22 09:06 iEricKoh

Sorry for a late response @iEricKoh :disappointed:

At first, sounds a bit like a bug with beamingHistory being cleared too much :thinking: There's a property in BeamerDelegate called removeDuplicateHistory. Try setting it to false and see do you observe the same behavior (or buggy in some other way). If yes, I think you can create a new issue for this.

Thanks!

slovnicki avatar Jul 06 '22 21:07 slovnicki

I have the same issue but workarounds didnt solved the problem. I am digging to get more info / more explanations

SOLVED :

added MaterialApp.router(

backButtonDispatcher: BeamerBackButtonDispatcher( delegate: routerDelegate as BeamerDelegate, ),

dleurs avatar Mar 03 '23 19:03 dleurs