0

My client claims that after putting the app to the background for 2-3mins and browsing other apps, and then getting back to my app again, the app is not restarting where it was left.

Suppose I'm in the middle of the checkout or payment screen, then the focus is taken to the home page after a while.

I don't need to use Services so not suffer of limitations on newer versions

can anyone suggest me what to do?

Edit 1: I've found PowerManager.WakeLock but, I don't know if it safe or handle my case or not

Moustafa EL-Saghier
  • 1,721
  • 1
  • 13
  • 43

1 Answers1

0

Don't setup wake lock or disable power optimization, a background activity will still be killed.

What you need is to simply save activity state on pause and re-create it on onCreate.

See Process Lifecycle:

A background activity (an activity that is not visible to the user and has been stopped) is no longer critical, so the system may safely kill its process to reclaim memory for other foreground or visible processes. If its process needs to be killed, when the user navigates back to the activity (making it visible on the screen again), its onCreate(Bundle) method will be called with the savedInstanceState it had previously supplied in onSaveInstanceState(Bundle) so that it can restart itself in the same state as the user last left it.

This means you need to preserve everything the users sees in your app - even things like zoom level, cursor and scroll position, etc.

For an example refer to How to save an activity state using save instance state?

rustyx
  • 80,671
  • 25
  • 200
  • 267
  • thank you for your timing invested to replay to me, but, I don't know which screen will user leave the app at wen put to the background, also, I can't save everything he types local because of payment info that can be done through the app. I know well about the `onSavedInstance` but won't help me, sorry to tell you that. – Moustafa EL-Saghier Apr 25 '20 at 09:27
  • Of course you know exactly which screen, it's your app. Just keep track of it and remember which screen the user is at. But sensitive info like payment is never saved, you can check any banking app - it will NOT be restored to where the user left off. – rustyx Apr 25 '20 at 11:15
  • i need it to be dynamic whatever screen, as not logical to do that in all app screens – Moustafa EL-Saghier Apr 25 '20 at 15:18