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

[Discussion] Be able to restore views (fragments) natively when Android kills the process #96

Closed
ferrannp opened this issue May 8, 2019 · 3 comments

Comments

@ferrannp
Copy link
Contributor

ferrannp commented May 8, 2019

I am opening this as a place for discussion instead of using Twitter. All this comes from my try & error + my experience using a RN app daily on my Android phone. Summary is here https://twitter.com/ferrannp/status/1126056440924389376.

There has been some discussion here too: #17

So there are two scenarios:

  1. An user kills the app (swipe up gesture)
  2. The system kills the app (saving its state) to free memory and when you come back to the app, everything is restored

To simulate number 2 you can:

  • Open a native app and navigate somewhere inside
  • Minimize it with the home button and then run: adb shell am kill app.package.name
  • Open the app again and views/navigation will be restored

After using a React Native app daily on my phone, I find 2) happening quite a lot (Pixel 2) when I open other apps and then I come back to mine.

I think as react-native-screens use fragments, it could help with this. Right now is not working, and that is why #17 happened. That's why we use the workaround:

@Override
protected void onCreate(Bundle savedInstanceState) {
  // null here = do not try to restore anything
  super.onCreate(null);
}

I believe we could persist fragments and let the system restore them. What are the problems with that? Maybe the state of JS won't be the same (for example, if you use Redux). However, I think this can be an alternative and document that if you persist native state, you are responsible to persist JS state (AsyncStorage or whatever you prefer).

What do you think?

cc @brentvatne @osdnk @kmagiera @satya164

@kmagiera
Copy link
Member

kmagiera commented May 8, 2019

Android fragments have built-in view hierarchy persistence by default. It is turned off in screens however. I believe this is outside of the scope of this project. The problem here is that there is no value in having your view hierarchy restored if the underlying react app does not restore to the same state. This way you'd have a UI out of sync with the application state. The way it should work is that app state persistence should be handled on the react side, such that when the app gets restored react app first recreates the state and only after that native view hierarchy should be recreated to match that state (this may not always be possible and in fact this is the same for native apps, I had many issues with that automatic mechanism in the past when working on Android native apps because normally only a fraction of the app state is kept in the view hierarchy)

@ferrannp
Copy link
Contributor Author

ferrannp commented May 8, 2019

The problem here is that there is no value in having your view hierarchy restored if the underlying react app does not restore to the same state.

But this could be done, right?

What if:

  1. There is an option to turn it on so fragments will be recreated (not sure what line we would need to change to try that right now?). This option could just be passing null or not to super.onCreate
  2. Document that is possible to save & restore whatever you want from JS. For example, one can just return null from the root component till you have restored the JS state (if you do not want to see inconsistencies)

Don't you think it can work?

@satya164
Copy link
Collaborator

satya164 commented May 8, 2019

React works terribly with rehydration even in React DOM. Highly doubt it's possible to make it work reliably here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants