Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Activity Lifecycle The Activity Lifecycle Retrieving Instance State

Where are these method called?

Once i created the "protected void onSaveInstanceState(Bundle outState)" and "protected void onRestoreInstanceState(Bundle savedInstanceState)" methodes i dont see where these are called and how i was supposed to know to create them because i just created them and for me they are simple methods that are never used. Where are these methods called?

1 Answer

Joe Brown
Joe Brown
21,465 Points

You use those methods as a way to save data between times when your Activity is destroyed and then recreated. The system calls those methods for you at the appropriate time. Before your Activity is going to be killed the system will call onSavedInstanceState(Bundle) where you can store any data in the Bundle object passed in you want to persist through to when the Activity is being recreated. Then when the system is recreating your Activity it will pass that same Bundle where you saved your data to the onRestoreInstanceState(Bundle) where you can pull out the data again and use it as need. That is also the same Bundle that is passed to onCreate(Bundle).. You should read this which breaks the whole process down https://developer.android.com/training/basics/activity-lifecycle/recreating.html