On Android the back button isn't closing the app entirely. It animates as it is, but the app can still be seen on multitasker. If you try to open the app again, it gives a white screen that never changes, and if I press back, I get the following error:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.reactnativenavigation.viewcontrollers.ViewController.handleBack(com.reactnativenavigation.viewcontrollers.Navigator$CommandListener)' on a null object reference
![]()
I can the setRoot method on my Navigation.events().onAppLaunched callback, think thats might be related because the app doesn't seems to understand that it is being launched again.
Do you have a registered screen before your main screen? Or are you instantiating the main screen directly from onAppLaunched?
@gabrielvcbessa Can you reproduce this issue in the playground app?
@guyca I could't, I'm gonna update my RNN version and see if it fix it. I'll keep you up to date
I found out what is the issue. Different from the playground app, I am using one reducer to determinate what will be the app's root, the logic is something like this:
handleStoreUpdate = () => {
const { app } = store.getState();
if (app.root && this.currentRoot !== app.root) {
this.changeAppRoot(app.root);
this.currentRoot = app.root;
}
};
My main goal is to change my app root only when the store root is updated. The problem is that when we close the app using the back button, the store state doesn't change, so, when I open it again, I get:
this.currentRoot === app.root /* equals true */
Can I detect if my app currently has a root or detect when the app is being closed? If so, I could do something like:
(!hasRoot || (app.root && this.currentRoot !== app.root))
Fixed. I just reset the root on app lauch:
Navigation.events().registerAppLaunchedListener(() => {
this.currentRoot = null; /* This fixed it */
...
});
@guyca @jinshin1013 Thanks for helping, now the issue can be closed! :)
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If you believe the issue is still relevant, please test on the latest version and report back. Thank you for your contributions.
@gabrielvcbessa Coluld you show me all your App.js file. I have a very similar problem, but i can't fix it. Thanks.
@gabrielvcbessa
What that it mean : this.currentRoot = null ???
@retyui, @erick94isc
Sorry for the late reply guys.
My app works in this way: For each time registerAppLaunchedListener is called, I call changeAppRoot. This method compares the current root of the App component (stored on this.currentRoot) with the recommended one that can be either 'logged-in' or 'logged-out', based on users authentication, and call the react-native-navigation 'startApp' method based on the desired app type.
When you close your app using the back button, the navigation stack is reseted, but all your variables and classes doesn't change. So, if you are logged in, for example, when opening the app again you would have:
this.currentRoot == 'logged-in' # true
So my method changeAppRoot wouldn't call startApp again, because the root didn't change, but the react-native-navigation was unmounted, so the app would be a blank screen. So, fixing it was simple as reseting this.currentRoot every time that registerAppLaunchedListener callback was called.
Does it make sense? If you have any other doubts, just ask, :)
@gabrielvcbessa
Thanks, now I understand what the problem is and how to solve it!)
Will you close issue?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If you believe the issue is still relevant, please test on the latest version and report back. Thank you for your contributions.
The issue has been closed for inactivity.
I am facing somewhat related issue when app is closed with Android back button and relaunched it seem's that Navigation.setDefaultOptions is not working anymore.
Set this.currentRoot got crash also. Anyone can give me another solution pls
@NirpE i have the same problem temporally Im fixing on this way
Navigation.events().registerAppLaunchedListener(() => {
initializeNavigation(); // this is where Navigation.setDefaultOptions is set
if (rootScreen) {
startSinglePageApp(screenNames[rootScreen]); // this is where Im call setRoot
}
});
Most helpful comment
@retyui, @erick94isc
Sorry for the late reply guys.
My app works in this way: For each time registerAppLaunchedListener is called, I call changeAppRoot. This method compares the current root of the App component (stored on this.currentRoot) with the recommended one that can be either 'logged-in' or 'logged-out', based on users authentication, and call the react-native-navigation 'startApp' method based on the desired app type.
When you close your app using the back button, the navigation stack is reseted, but all your variables and classes doesn't change. So, if you are logged in, for example, when opening the app again you would have:
So my method changeAppRoot wouldn't call startApp again, because the root didn't change, but the react-native-navigation was unmounted, so the app would be a blank screen. So, fixing it was simple as reseting this.currentRoot every time that registerAppLaunchedListener callback was called.
Does it make sense? If you have any other doubts, just ask, :)