{"id":329,"date":"2015-12-18T05:41:14","date_gmt":"2015-12-18T05:41:14","guid":{"rendered":"http:\/\/abhiandroid.com\/programming\/?p=329"},"modified":"2018-06-06T04:55:50","modified_gmt":"2018-06-06T04:55:50","slug":"oncreate-activity-android","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/programming\/oncreate-activity-android.html","title":{"rendered":"onCreate(Bundle savedInstanceState) Activity Function And Example In Android"},"content":{"rendered":"<h4><strong>onCreate(Bundle savedInstanceState) Function in Android:<\/strong><\/h4>\n<ul>\n<li>When an Activity first call or launched then onCreate(Bundle savedInstanceState) method is responsible to create the activity.<\/li>\n<li>When ever orientation(i.e. from horizontal to vertical or vertical to horizontal) of activity gets changed or when an Activity gets forcefully terminated by any Operating System then savedInstanceState i.e. object of Bundle Class will save the state of an Activity.<\/li>\n<li>After Orientation changed then onCreate(Bundle savedInstanceState) will call and recreate the activity and load all data from savedInstanceState.<\/li>\n<li>Basically Bundle class is used to stored the data of activity whenever above condition occur in app.<\/li>\n<li>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.<\/li>\n<li>You could also put your initialization code in onStart() or onResume() and when you app will load first, it will work same as in onCreate().<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>Example of onCreate(Bundle savedInstanceState):<\/strong><\/h4>\n<p>Lets create a simple program to understand onCreate(Bundle savedInstanceState) more deeply.<\/p>\n<p>In the below example we will ask the user to Enter the name, save his name in Bundle object and then display the same name when user rotates the screen.<\/p>\n<p>First create a new project, name it Exampleoncreate and create a activity name MainActivity. Also create a content_main.xml in layout if not present by default.<\/p>\n<p>For designing UI we will create 3 things in content_main.xml:<\/p>\n<ul>\n<li>We have used Linear Layout<\/li>\n<li>a TextView to display basic detail about the program to user,<\/li>\n<li>EditText where user will enter the name which will be save in Bundle object<\/li>\n<li>a TextView which will display the name on screen rotation<\/li>\n<\/ul>\n<p><strong>Below is the complete code of content_main.xml<\/strong><\/p>\n<pre>&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"fill_parent\" &gt;\r\n\r\n    &lt;TextView\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:textAppearance=\"?android:attr\/textAppearanceMedium\"\r\n        android:text=\"Fill Name Then Rotate:\"\r\n        android:id=\"@+id\/textView2\" \/&gt;\r\n\r\n\r\n    &lt;!-- used to enter the data and get saved in bundle--&gt;\r\n\r\n    &lt;EditText\r\n        android:id=\"@+id\/editText\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:inputType=\"text\"\r\n\r\n        \/&gt;\r\n\r\n\r\n    &lt;!-- retrived data shown in this textview --&gt;\r\n\r\n    &lt;TextView\r\n\r\n        android:id=\"@+id\/textView\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        \/&gt;\r\n\r\n\r\n\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p><strong>Now go to MainActivity.java and paste the below code. The explanation is given using comments in the code itself.<\/strong><\/p>\n<pre>import android.os.Bundle;\r\nimport android.support.design.widget.FloatingActionButton;\r\nimport android.support.design.widget.Snackbar;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.support.v7.widget.Toolbar;\r\nimport android.util.Log;\r\nimport android.view.View;\r\nimport android.view.Menu;\r\nimport android.view.MenuItem;\r\nimport android.widget.EditText;\r\nimport android.widget.TextView;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    private static String TAG = \"ActivityName\";\r\n    public void onCreate(Bundle savedInstanceState)\r\n    {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.content_main);\r\n    }\r\n\r\n\/\/Saving The Text Entered by User\r\n\r\n    protected void onSaveInstanceState(Bundle outState)\r\n    {\r\n        super.onSaveInstanceState(outState);\r\n        Log.i(TAG, \"onSaveInstanceState\");\r\n\r\n        final EditText editText=\r\n                (EditText) findViewById(R.id.editText);\/\/ getting the reference of editext from xml\r\n        CharSequence text  = editText.getText();\/\/ getting text u entered in edittext\r\n        outState.putCharSequence(\"savedText\", text);\/\/ saved that text in bundle object i.e. outState\r\n\r\n    }\r\n\r\n\/\/Restoring the State\r\n\r\n    @Override\r\n    protected void onRestoreInstanceState(Bundle savedInstanceState)\r\n    {\r\n        super.onRestoreInstanceState(savedInstanceState);\r\n        Log.i(TAG, \"onRestoreInstanceState\");\r\n        final TextView textView =\r\n                (TextView) findViewById(R.id.textView);\/\/ getting the reference of textview from xml\r\n\r\n        CharSequence savedText=\r\n                savedInstanceState.getCharSequence(\"savedText\");\/\/ getting the text of editext\r\n\r\n        textView.setText(savedText);\/\/ set the text that is retrieved from bundle object\r\n    }\r\n\r\n}\r\n<\/pre>\n<p><strong>Make sure MainActivity is mentioned in Manifest file. Below is the code of AndroidManifest.xml<\/strong><\/p>\n<pre>&lt;manifest xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    package=\"abhiandroid.com.exampleoncreate\"&gt;\r\n\r\n    &lt;application\r\n        android:allowBackup=\"true\"\r\n        android:icon=\"@mipmap\/ic_launcher\"\r\n        android:label=\"@string\/app_name\"\r\n        android:supportsRtl=\"true\"\r\n        android:theme=\"@style\/AppTheme\"&gt;\r\n        &lt;activity\r\n            android:name=\".MainActivity\"\r\n            android:label=\"@string\/app_name\"\r\n            android:theme=\"@style\/AppTheme.NoActionBar\"&gt;\r\n            &lt;intent-filter&gt;\r\n                &lt;action android:name=\"android.intent.action.MAIN\" \/&gt;\r\n\r\n                &lt;category android:name=\"android.intent.category.LAUNCHER\" \/&gt;\r\n            &lt;\/intent-filter&gt;\r\n        &lt;\/activity&gt;\r\n    &lt;\/application&gt;\r\n\r\n&lt;\/manifest&gt;<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Now run the application in Emulator and enter the name. In our case we have entered AbhiAndroid.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-340\" src=\"\/programming\/wp-content\/uploads\/2015\/12\/Fill-name-in-oncreate-example.jpg\" alt=\"Fill name in oncreate example\" width=\"357\" height=\"185\" srcset=\"https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/Fill-name-in-oncreate-example.jpg 357w, https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/Fill-name-in-oncreate-example-300x155.jpg 300w\" sizes=\"auto, (max-width: 357px) 100vw, 357px\" \/><\/center><\/p>\n<p>Now rotate the screen from vertical to horizontal (Press ctrl + F12 in Windows &amp; Fn+Left CTRL+F12 on Mac to rotate). After you change screen orientation you will see the same name appear just right side of name.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-341\" src=\"\/programming\/wp-content\/uploads\/2015\/12\/Output-change-screen-orientation.jpg\" alt=\"Output change on screen orientation\" width=\"690\" height=\"184\" srcset=\"https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/Output-change-screen-orientation.jpg 690w, https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/Output-change-screen-orientation-300x80.jpg 300w\" sizes=\"auto, (max-width: 690px) 100vw, 690px\" \/><\/center><\/p>\n<p>So we have retrieved the data from Bundle object.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>onCreate(Bundle savedInstanceState) Function in Android: When an Activity first call or launched then onCreate(Bundle savedInstanceState) method is responsible to create the activity. When ever orientation(i.e. from horizontal to vertical or vertical to horizontal) of activity gets changed or when an Activity gets forcefully terminated by any Operating System then savedInstanceState i.e. object of Bundle Class &hellip; <a href=\"https:\/\/abhiandroid.com\/programming\/oncreate-activity-android.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">onCreate(Bundle savedInstanceState) Activity Function And Example In Android<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62,1],"tags":[],"class_list":["post-329","post","type-post","status-publish","format-standard","hentry","category-android-methods","category-archieve"],"psp_head":"<title>onCreate(Bundle savedInstanceState) Activity Function And Example In Android \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn the different function of onCreate(Bundle savedInstanceState) of Activity lifecycle with example in Android. When an Activity first call or launched then onCreate(Bundle savedInstanceState) method is responsible to create the activity.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/programming\/oncreate-activity-android.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/posts\/329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/comments?post=329"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/posts\/329\/revisions"}],"predecessor-version":[{"id":917,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/posts\/329\/revisions\/917"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/media?parent=329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/categories?post=329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/tags?post=329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}