12

I am new to Android Development but I have some knowledge of OOP concepts. I am trying to learn Android from Java perspective.

My code

I understand that savedInstanceState inside onCreate() on line 9 is the declaration of Bundle class. On line 10 we are calling the onCreate() method from the super class.

Here is what I don't unterstand: On line 10 we pass savedInstanceState itself as a parameter to the onCreate() method. This doesn't make sense to me as I would expect to pass an object which is of the Bundle type, but instead of that we pass a reference not an object of type Bundle to the method.

Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
amitoz
  • 313
  • 2
  • 4
  • 13
  • 3
    I think your confusion has nothing to do with the code, but everything with your understanding of references and objects. `onCreate()` expects a `Bundle` parameter, savedInstanceState is a `Bundle` so there is no issue here. – Xaver Kapeller May 11 '16 at 03:30
  • See here http://stackoverflow.com/a/10810447/5202007 Or https://blog.pivotal.io/labs/labs/android-savedinstancestate-bundle-faq Or http://stackoverflow.com/questions/151777/saving-activity-state-on-android Or http://stackoverflow.com/questions/9846817/when-is-the-savedinstancestate-bundle-actually-used – Mohammad Tauqir May 11 '16 at 03:31
  • @Tauqir I think you are completely missing the point of the question. – Xaver Kapeller May 11 '16 at 03:35
  • And by the way: Please edit your code directly into your question. Never post pictures of your code here. – Xaver Kapeller May 11 '16 at 03:36
  • 1
    To go into more detail than my first comment: the `Bundle savedInstanceState` part in line 9 is the declaration of a `Bundle` parameter for the `onCreate()` method. That means the `onCreate()` expects to be called with a `Bundle` instance as parameter. In line 10 this parameter then gets passed to the overridden method of the parent class. – Xaver Kapeller May 11 '16 at 03:41
  • @XaverKapeller In line 9 there is declaration of Bundle type parameter. That means only a reference is created i.e. *savedInstanceState*, only a name is given. As you said in line 10 it expects to pass a Bundle instance, ** not the reference** in *onCreate* method. But we are passing only reference in it. Because no where I see this *savedInstanceState* to be instantiated as Bundle object. I only see a declaration in line 9. Do you see my point? – amitoz May 11 '16 at 06:22
  • Possible duplicate of [What's onCreate(Bundle savedInstanceState)](http://stackoverflow.com/questions/10810418/whats-oncreatebundle-savedinstancestate) – Mayur Sakhiya May 11 '16 at 06:58
  • @XaverKapeller, Thank you for pointing out the issue. I gave references only to read more about `savedInstanceState`, so that it becomes more clear to @amitoz. The links don't answer the question fully. – Mohammad Tauqir May 11 '16 at 07:14

3 Answers3

17

The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity.

The onCreate() expects to be called with a Bundle as parameter so we pass savedInstanceState.

Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle. If there is no available instance data, the savedInstanceState will be null.

For example, the savedInstanceState will always be null the first time an Activity is started, but may be non-null if an Activity is destroyed during rotation, because onCreate is called each time activity starts or restarts.

Hope it helps

Atiq
  • 14,435
  • 6
  • 54
  • 69
3

When an activity is re-initialized ,then the savedInstance contains the most recent data , specially contains data of the activity's previous initialization part.

Actually, when an activity is killed it saved its states so that when user back to this activity , it can restore its states

Atahar Hossain
  • 336
  • 3
  • 11
0

Basically bundle is a type of data structure which stores the data which was used previously in the activity..When there is some disturbance by the system or suppose some called u and u picked up phone then the data u had in ur app kept stored in bundle and you can access the same data..

And if we talk about savedinstancestate then it is very easy to know that basically savedinstancestate is an object/reference what ever doesn't matter but it is the way by which data will be kept stored...As u must know that class is just a prototype or blueprint (you may say) savedinstancestate is an object or reference.. Bundle is a class I think u know that..