Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I save the application back stack to a bundle?

I'd like to save the state of my application so that when it is reopened from a closed state, the last fragment is visible and the back stack is preserved. I'd like to preserve this state every time the application is closed, not just on an orientation change or when the system kills the app to free up resources (as is the case when using onSaveInstanceState()/onRestoreInstanceState().

So far I've been able to restore the previous fragment and its state as planned by saving the state to SharedPreferences and restoring it later. However, this does not preserve the fragment back stack, so when the user loads the application and presses the back button to move "up" in the application flow, the app quits instead of going up.

What I need at this point is to be able to get access to the application back stack and save it to a bundle when necessary, much the way the system does in onSaveInstanceState(). I've read the documentation for FragmentManager.saveFragmentInstanceState() but I'm not sure how to use this to accomplish my goal. Can anybody point me in the right direction?

I'm using the fragment implementation from the Android compatibility library.

like image 938
howettl Avatar asked Mar 26 '12 20:03

howettl


1 Answers

As far as I know Android system won't let you get the task's back stack, it can only give you the top Activity of all tasks running. Hence, you will need to implement your own mechanism of maintaining the Activities back stack. Implementation of LocalActivityManager might help you, particularly, its saveInstanceState() method. So you may do something similar and call onSaveInstanceState(Bundle) of Activities in your back stack whenever you want and then persist Bundle (in a file, for example). If you use Fragments then you will also need to maintain their back stack.

Perhaps, android:alwaysRetainTaskState might help you to solve your problem not diving into managing back stack.

like image 66
a.ch. Avatar answered Oct 23 '22 05:10

a.ch.