Issue
I'm developing an Android application and I would like to emulate the same behavior of the official Google Plus app when it is opened, which is:
- If the app is still in the Recent Apps list and you open it (from icon or from recent apps, it doesn't matter) you will resume the app from where you left.
- If the app is no longer in the Recent Apps list and you open it, you will be greeted with a splash screen and then directed to the Home page.
My app has one activity that uses a navigation drawer and several fragments as pages. My goal is to retain the current fragment and display it when the app is resumed from the Recent Apps. I can manage to do so using SharedPreferences by storing the tag of the current fragment and loading it when the user reopens the app. The problem with is solution is that this kind of data is persistent so the user will always be greeted to the last page even if he first removes the app from the Recent Apps list and reopens it.
So how can I detect whether the app is already in the Recent Apps list or not? Or is there any method that is called when an app is removed from that list so I can clear the last fragment tag from SharedPreferences? Or am I using the wrong approach?
Thank you for you patience in reading this.
Solution
When you return to your app through the recent-tasks list, there are three possibilities:
It has been a long time, over 30 minutes, and your process is not running. In that case, Android just launches your app as if the user tapped on your home screen launcher icon. You are not given your saved instance state, as it is deemed to be stale.
It has not been a long time, so Android wants the user to think that your app has been running since they left it. However, due to memory pressure, Android terminated your process while it was in the background. In this case, Android forks a new process for you, creates an instance of your activity, and passes to you your saved instance state
BundleinonCreate()andonRestoreInstanceState().Your process is already running. In that case, your activity is just brought back to the foreground. You do not need your instance state, as your activity instance is still running. Android has not touched your widgets, and so if your UI is not the way you want it to be, that is your own fault, because you did something (e.g., in
onPause()oronStop()) that screwed up your UI. From your descriptions, I am interpreting it that you are testing this scenario, in which caseonRestoreInstanceState()is not called, because it is not needed.
So, as long as you do not screw up your own UI, your app will work as you described it for Google Plus, so long as you handle the saved instance state for scenario #2 above.
On Android 5.0+, there are related scenarios tied into the PersistableBundle that you can use with variations of onSaveInstanceState() and kin. I have not played with the PersistableBundle much and so I do not have any particular advice related to it.
Answered By - CommonsWare
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.