Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you please explain onCreate and Bundles?

I have been looking it up and I just cant seem to wrap myself around the onCreate and Bundles. I understand that the onCreate is called when the program starts but its how the Bundles get passed around and how they are pertinent. Can anyone try to put this into plain english because I cant seem to find it well described.

Thanks

like image 531
Mike Avatar asked May 22 '10 07:05

Mike


People also ask

What is onCreate?

onCreate() is called when the when the activity is first created. onStart() is called when the activity is becoming visible to the user.

What is the purpose of the bundle argument to activity onCreate?

Basically Bundle class is used to stored the data of activity whenever above condition occur in app. onCreate() is not required for apps. But the reason it is used in app is because that method is the best place to put initialization code.

What is the onCreate method?

onCreate(Bundle savedInstanceState) Function in Android: When an Activity first call or launched then onCreate(Bundle savedInstanceState) method is responsible to create the activity.

What is onCreate bundle savedInstanceState in Android?

What is the savedInstanceState Bundle? The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity. Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle.


1 Answers

The Bundle in the onCreate method should hold the state of you activity before it was killed.

Simple example, when you change the orientation of your device your activity is recreated. Imagine the user is filling a long form and he/she accidentally changes the orientation. When the app gets restarted all data entered will be lost unless you persist that information. One possibility is using a Bundle.

If you want to know how to use it, I would recommend that you read this question.

like image 53
Macarse Avatar answered Nov 13 '22 17:11

Macarse