Ionic foreground and background - store state of application

Please describe the question in detail and share your code, configuration, and other relevant info. I have an issue with the app and background working. Can anybody help me?

I made an app for the clinic and all works well while they are in-app but when for example pick up the phone or want to reply to a message looks the state of the app lose and they can’t navigate through the application (the back button dissipated) and need to kill the app and start it again. Is there any solution to it?

I made a “hack” in the app.component.ts file:

App.addListener('appStateChange', ({isActive}) => {
  if (!isActive) {
    // App went to background
  } else {
    // App went to foreground

but when I add that user can’t take a picture of the patient or change his avatar, it always backs him on the homepage :frowning: I use a capacitor to build apps. Is there any solution to save the state to not lose on which page the user exists and where he can go back?

For my Ionic Vue app, I use a combination of the Capacitor Preferences plugin and Ionic Storage to store state. While the app is running, Vuex (Vue’s older state management) is used which is synced with the aforementioned storage so Vuex can be re-populuated on a cold start of the app.

I don’t believe you can control when Android or iOS decides to kill the app. It’s all based on current resources and power saving. If you need the app to return to where the user was on a cold start, then you would probably need to store the current page they are on using one of the mentioned plugins and upon app start, get their last page and redirect them there.

1 Like

@twestrick thank you for your comment. Do you have some example/demo/tutorial for it?

You can check out this post where I have some more comments and shared some code.

Do you happen to know if there is a way to replicate the device killing the app?
It would be useful to be able to test the recovery code.

I only have a handful of production users experiencing this and I can’t seem to replicate on my devices.

I don’t think I have anything more to add than what hills90210 said in your other post.

Swiping away in recent apps doesn’t replicate killing the app? Or maybe Force Stop in Android’s app settings?

How are you storing your app’s data?

The apps data is just stored in variables in the app itself which get wiped. At this stage I don’t store them in local storage or anything. I have done this in other apps though.

I still have the firebase user though as this is stored in local storage and retains the data so I can just restart the app on the home page with the user logged in.

Swiping away does seem to replicate the reset of the app when opened again but I can’t replicate the error that some users are getting. I will have more of a play with added logging and see if I can figure it out.

My devices don’t seem to have the issue if left for several days either so swiping away could very well replicate the same process but it may depend on the specific device or local connection status.

What error are these affected users seeing? Is it a network error as alluded to in your Network.getStatus post?

I’ve only created one app so I am no guru, but I feel like caching data locally is going to be your best bet in order to provide your users the best experience. Then on app load, just do a refresh of data. Or if it is critical for the user to only see fresh data, on app open, show a loading screen and have some error handling just in case the network isn’t available or takes a bit to become active again.

The error does look like it may be a network error. When the network comes back I go to get the latest data but encounter a network error when get the token from the firebase user (user.getIdTokenResult(true))

For now I have just added a delay before refreshing the data when the app is restarted / network is reconnected.
I have also added Sentry to track the errors on the users devices.
Once I have more data I will dig into it some more.

The primary issue is I’m running blind as struggling to find a way to replicate the issue hopefully the error logs will help with this.

1 Like