Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to add Android Back Handler for a perticular screen? #15248

Closed
rajsuvariya opened this issue Jul 28, 2017 · 9 comments
Closed
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@rajsuvariya
Copy link
Contributor

rajsuvariya commented Jul 28, 2017

I have three screens MyVault, Add Doc and Add Repo. From Myvault there is one button add new doc by clicking on that Add Doc will open. Now in Add doc if user presses Bank button then I want a confirmation pop up. I have one button inside add doc screen which opens Add repo screen where user can select one repo and when they click add that screen will be poped and add doc screen will be refreshed with the repo data. If I add a listener in ComponentDidMount and then remove it in ComponentWillUnmount then the issue is that even when I press back on Add repo then also the popup comes. I don't want popup on any other screens, I just want it on Add doc.

@dopey2
Copy link

dopey2 commented Jul 28, 2017

How do you handle your navigation betwen scene? Are you using react-navigation or router-flux ?

@rajsuvariya
Copy link
Contributor Author

rajsuvariya commented Jul 28, 2017

@Dani93380 I am using react-native-router-flux v4 which is a wrapper on react-navigation only

@dopey2
Copy link

dopey2 commented Jul 28, 2017

You should have 3 class for your 3 components . If you add the listener inside the AddDoc class only and remove it on componentWillUnmount it should work.

@rajsuvariya
Copy link
Contributor Author

rajsuvariya commented Jul 28, 2017

Actually, I have done the same but what happens is, this event listener if not removed will be there on all the screens.
I have added this listener on Add doc and works fine when I press back. But now when I click on add repo button it will open Select Repo screen where a user can select repo and come back to add doc. So when user opens the Select Repo componentWillUnmount won't be called, so the listener will be there and will open popup when user press back on Select Repo screen which is not the desired behavior

@dopey2
Copy link

dopey2 commented Jul 28, 2017

If you are using redux or something similar , store your current scene in the store and only call your back function if currentScene=="AddDoc"

@rajsuvariya
Copy link
Contributor Author

rajsuvariya commented Jul 28, 2017

No I am not using redux or anything. But I think react native should provide such options

@dopey2
Copy link

dopey2 commented Jul 28, 2017

@rajsuvariya
Copy link
Contributor Author

rajsuvariya commented Jul 28, 2017

Never mind figured out that react-native-router-flux has onEnter and onExit callbacks for every scene. So I added the event listener in onEnter and removed it in onExit and that fixed my issue.

@rajsuvariya
Copy link
Contributor Author

BTW @Dani93380 I know about Redux but don't want to use in my app :D

@facebook facebook locked as resolved and limited conversation to collaborators Jul 28, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 28, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

3 participants