We're not quite done yet. We making sure to save data when the Activity is destroyed. But we're not getting the data back anywhere.
We can do that in the onCreate method. When an app is first created, the savedInstanceState variable between the round brackets of onCreate will be null. It's only not null when we start adding things to the Bundle object. We can test for this null state. Add this to you onCreate method, just before the final curly bracket:. To the left of an equal sign, we have our someScore variable. To the right, we're calling a method on Bundle that has the name savedInstanceState.
This method is getInt. This is used to get an integer value from the Bundle. If you had saved a string value then you'd use getString. After a comma, you can type a default value, just in case something goes wrong. To achieve this, we have to override a method called onSaveInstanceState. Android platform allows the users to save any instance state. Instance state can be saved in the Bundle.
Bundle is passed as argument to the onSaveInstanceState method. We can also load the saved instance state in onRestoreInstanceState method. But I will leave that for the readers to figure out.
As Dhruv Gairola answered, you can save the state of the application by using Bundle savedInstanceState. I am trying to give a very simple example that new learners like me can understand easily. Suppose, you have a simple fragment with a TextView and a Button. Is there any documentation? One instance I can think of is if the user switches apps and comes back, but the activity has been destroyed for recovering resources.
In this case android will re-create the activity with a bundle if onSaveInsanceState was overriden, right? I guess the best way to put it is the Bundle argument to onCreate Bundle will be non- null if and only if the Activity had it's onSaveInstanceState method previously called.
This happens during orientation changes Thanks for the clarification Alex. It's good to have some clarity on the subject. I'd forgotten about configuration changes other than orientation switches in particular. Asad Asad 1, 12 12 silver badges 19 19 bronze badges.
Notice that this also holds true on Activity. Can you explain more on the difference of Bundle of Activity and Bundle of Fragment? You can get a clear understanding from the life cycles of activity and fragment where a activity can display multiple fragments at a time. So, the basic difference is that you may want to save some values for all the fragments of an activity, then u will use the bundle of activity otherwise u should use bundle of fragment if it is for specific fragment.
Abhi Abhi 2, 21 21 silver badges 14 14 bronze badges. The Overflow Blog. Podcast Explaining the semiconductor shortage, and how it might end. Does ES6 make JavaScript frameworks obsolete? Featured on Meta. Now live: A fully responsive profile. Visit chat. Linked 1. See more linked questions. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled.
0コメント