Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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