Also onSaveInstanceState() will definitely be called when your activity is in the foreground and you press home. In that case the activity is not being finished, so the user may return to the same instance later (in the same state as they left it), so onSaveInstanceState() will be called before onPause() in case its process gets kill any time after onPause() returns.
On Sun, Jul 5, 2009 at 11:25 AM, Peli <[email protected]> wrote: > > > onSavedInstanceState() > > First make sure you don't have a typo, because it is called > onSaveInstanceState(...). > > A case where it is definitely called is when you switch orientation > (in the G1, slide out the keyboard). > > Then onSaveInstanceState(..) is called 100%, and onCreate(...) in the > newly created activity contains the bundle that you stored yourself. > (provided you let the system handle the screen orientation change). > > Does this work? It should. > > If you call "finish()" or press "Back", then onSaveInstanceState() is > NOT called, because it is assumed the user finishes the activity. If > the user opens the activity again, a fresh instance is created. > > If you think the current directory should be the same after you press > finish() and launch the application again, then it is NOT a candidate > for saving in the instance state. > > If you only want to keep the current directory when changing screen > orientation, or being interrupted by an incoming call, then > saveInstanceState IS the correct thing for you. > > Peli > www.openintents.org > > On 5 Jul., 15:32, sasq <[email protected]> wrote: > > The documentation makes it sound that it would normally BE called, > > thats why I was wondering. > > In my case I have a second Activty that I open from the Main Activity, > > and regardess of wether I call finish() myself or use HOME to switch > > to another application, onSavedInstanceState() is never called. > > > > I guess saving to a SharedPreference in onPause() would fit best in my > > case (even though I dont need it shared). > > > > On Jul 5, 12:53 pm, Mark Murphy <[email protected]> wrote: > > > > > > > > > sasq wrote: > > > > Am I right to assume that onSaveInstanceState() is not normally > called > > > > (which my logging indicates) - but only when the system is forced to > > > > kill an Activity. > > > > > To quote the documentation: > > > > > "...for those methods that are marked as being killable, after that > > > method returns the process hosting the activity may killed by the > system > > > at any time without another line of its code being executed. Because of > > > this, you should use the onPause() method to write any persistent data > > > (such as user edits) to storage. In addition, the method > > > onSaveInstanceState(Bundle) is called before placing the activity in > > > such a background state, allowing you to save away any dynamic instance > > > state in your activity into the given Bundle, to be later received in > > > onCreate(Bundle) if the activity needs to be re-created... Note that it > > > is important to save persistent data in onPause() instead of > > > onSaveInstanceState(Bundle) because the later [sic] is not part of the > > > lifecycle callbacks, so will not be called in every situation as > > > described in its documentation." > > > > >http://developer.android.com/reference/android/app/Activity.html > > > > > > If so, since onPause() does not receive a bundle, what is the best > way > > > > to save state that should be restored the next time the Activity is > > > > created ? > > > > > A database. > > > > > Or, in an ordinary file in the application's file storage area (e.g., > > > openFileOutput()). > > > > > Or, inside of some SharedPreferences. > > > > > Or, in "the cloud" (i.e., store it out on the Internet somewhere, if > you > > > have connectivity). > > > > > Or, if all you are worried about is screen rotations, use > > > onRetainNonConfigurationInstance(). > > > > > Or, if you don't need the state to be stored on-disk, have a local > > > Service or custom Application object hold onto it. > > > > > Which of these (and any others I didn't think of off the top of my > head) > > > is "best" is really up to you, based upon your application and its > > > requirements. > > > > > -- > > > Mark Murphy (a Commons Guy)http://commonsware.com| > http://twitter.com/commonsguy > > > > > Warescription: Three Android Books, Plus Updates, $35/Year > > > -- Dianne Hackborn Android framework engineer [email protected] Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---

