{"id":2419,"date":"2017-04-24T09:51:59","date_gmt":"2017-04-24T09:51:59","guid":{"rendered":"http:\/\/webartdevelopers.com\/?p=2419"},"modified":"2017-04-24T10:00:21","modified_gmt":"2017-04-24T10:00:21","slug":"android-activity-lifecycle","status":"publish","type":"post","link":"https:\/\/webartdevelopers.com\/blog\/android-activity-lifecycle\/","title":{"rendered":"Android Activity Lifecycle"},"content":{"rendered":"<p>In this tutorial Android Activity Life Cycle \u00a0methods are shown.<\/p>\n<p>There are mainly 4 states of Activity, they are as below:<\/p>\n<ul>\n<li>Running: Activity is visible and interacts with the user<\/li>\n<li>Paused: Activity is still visible but partially obscured, instance is running but might be killed by the system<\/li>\n<li>Stopped: Activity is not visible, instance is running but might be killed by \u00a0the system<\/li>\n<li>Killed: Activity has been terminated by the system or by a call to its finish() method<\/li>\n<\/ul>\n<p>There are 7 methods of Activity Life Cycle they are as below:<\/p>\n<ol>\n<li>onCreate(): It is called once when your activity is created<\/li>\n<li>onStart(): It is called after onCreate() when your activity is started<\/li>\n<li>onPause(): It is called when you minimise your current \u00a0activity and open other activity<\/li>\n<li>onResume(): It is called whenever you resume your activity which was paused<\/li>\n<li>onStop(): It is called whenever you close your activity and whenever your activity is no longer visible<\/li>\n<li>onRestart(): It is called whenever you restart the application<\/li>\n<li>onDestroy(): It is called once when you destroy or kill your application<\/li>\n<\/ol>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"https:\/\/www.javatpoint.com\/images\/androidimages\/Android-Activity-Lifecycle.png\" width=\"523\" height=\"673\" \/><\/p>\n<p>Code:<\/p>\n<p>activity_main.xml<\/p>\n<p>&lt;RelativeLayout xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221;<br \/>\nxmlns:tools=&#8221;http:\/\/schemas.android.com\/tools&#8221;<br \/>\nandroid:layout_width=&#8221;match_parent&#8221;<br \/>\nandroid:layout_height=&#8221;match_parent&#8221;<br \/>\nandroid:paddingBottom=&#8221;@dimen\/activity_vertical_margin&#8221;<br \/>\nandroid:paddingLeft=&#8221;@dimen\/activity_horizontal_margin&#8221;<br \/>\nandroid:paddingRight=&#8221;@dimen\/activity_horizontal_margin&#8221;<br \/>\nandroid:paddingTop=&#8221;@dimen\/activity_vertical_margin&#8221;<br \/>\ntools:context=&#8221;.MainActivity&#8221; &gt;<\/p>\n<p>&lt;TextView<br \/>\nandroid:id=&#8221;@+id\/textView1&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignParentTop=&#8221;true&#8221;<br \/>\nandroid:layout_centerHorizontal=&#8221;true&#8221;<br \/>\nandroid:layout_marginTop=&#8221;60dp&#8221;<br \/>\nandroid:text=&#8221;TextView&#8221; \/&gt;<\/p>\n<p>&lt;TextView<br \/>\nandroid:id=&#8221;@+id\/textView2&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignLeft=&#8221;@+id\/textView1&#8243;<br \/>\nandroid:layout_below=&#8221;@+id\/textView1&#8243;<br \/>\nandroid:layout_marginTop=&#8221;21dp&#8221;<br \/>\nandroid:text=&#8221;TextView&#8221; \/&gt;<\/p>\n<p>&lt;TextView<br \/>\nandroid:id=&#8221;@+id\/textView3&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignLeft=&#8221;@+id\/textView2&#8243;<br \/>\nandroid:layout_below=&#8221;@+id\/textView2&#8243;<br \/>\nandroid:layout_marginTop=&#8221;26dp&#8221;<br \/>\nandroid:text=&#8221;TextView&#8221; \/&gt;<\/p>\n<p>&lt;TextView<br \/>\nandroid:id=&#8221;@+id\/textView4&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignLeft=&#8221;@+id\/textView3&#8243;<br \/>\nandroid:layout_below=&#8221;@+id\/textView3&#8243;<br \/>\nandroid:layout_marginTop=&#8221;26dp&#8221;<br \/>\nandroid:text=&#8221;TextView&#8221; \/&gt;<\/p>\n<p>&lt;TextView<br \/>\nandroid:id=&#8221;@+id\/textView5&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignLeft=&#8221;@+id\/textView4&#8243;<br \/>\nandroid:layout_below=&#8221;@+id\/textView4&#8243;<br \/>\nandroid:layout_marginTop=&#8221;24dp&#8221;<br \/>\nandroid:text=&#8221;TextView&#8221; \/&gt;<\/p>\n<p>&lt;TextView<br \/>\nandroid:id=&#8221;@+id\/textView6&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignLeft=&#8221;@+id\/textView5&#8243;<br \/>\nandroid:layout_below=&#8221;@+id\/textView5&#8243;<br \/>\nandroid:layout_marginTop=&#8221;35dp&#8221;<br \/>\nandroid:text=&#8221;TextView&#8221; \/&gt;<\/p>\n<p>&lt;TextView<br \/>\nandroid:id=&#8221;@+id\/textView7&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignLeft=&#8221;@+id\/textView6&#8243;<br \/>\nandroid:layout_below=&#8221;@+id\/textView6&#8243;<br \/>\nandroid:layout_marginTop=&#8221;32dp&#8221;<br \/>\nandroid:text=&#8221;TextView&#8221; \/&gt;<\/p>\n<p>&lt;\/RelativeLayout&gt;<\/p>\n<p>MainActivity.java<\/p>\n<p>package com.example.demo;<\/p>\n<p>import android.os.Bundle;<br \/>\nimport android.app.Activity;<br \/>\nimport android.util.Log;<br \/>\nimport android.view.Menu;<br \/>\nimport android.widget.TextView;<br \/>\npublic class MainActivity extends Activity {<br \/>\nTextView tv1,tv2,tv3,tv4,tv5,tv6,tv7;<\/p>\n<p>@Override<br \/>\nprotected void onCreate(Bundle savedInstanceState) {<br \/>\nsuper.onCreate(savedInstanceState);<br \/>\nsetContentView(R.layout.activity_main);<br \/>\ntv1=(TextView)findViewById(R.id.textView1);<br \/>\ntv2=(TextView)findViewById(R.id.textView2);<br \/>\ntv3=(TextView)findViewById(R.id.textView3);<br \/>\ntv4=(TextView)findViewById(R.id.textView4);<br \/>\ntv5=(TextView)findViewById(R.id.textView5);<br \/>\ntv6=(TextView)findViewById(R.id.textView6);<br \/>\ntv7=(TextView)findViewById(R.id.textView7);<\/p>\n<p>tv1.setText(&#8220;onCreate invoked&#8221;);<br \/>\nLog.d(&#8220;Lifecycle&#8221;,&#8221;onCreate invoked&#8221;);<br \/>\n}<\/p>\n<p>@Override<br \/>\nprotected void onStart()<br \/>\n{<br \/>\nsuper.onStart();<br \/>\ntv2.setText(&#8220;onStart invoked&#8221;);<br \/>\nLog.d(&#8220;Lifecycle&#8221;,&#8221;onStart invoked&#8221;);<\/p>\n<p>}<\/p>\n<p>@Override<br \/>\nprotected void onResume()<br \/>\n{<br \/>\nsuper.onResume();<br \/>\ntv3.setText(&#8220;onResume invoked&#8221;);<br \/>\nLog.d(&#8220;Lifecycle&#8221;,&#8221;onResume invoked&#8221;);<\/p>\n<p>}<\/p>\n<p>@Override<br \/>\nprotected void onPause()<br \/>\n{<br \/>\nsuper.onPause();<br \/>\ntv4.setText(&#8220;onPause invoked&#8221;);<br \/>\nLog.d(&#8220;Lifecycle&#8221;,&#8221;onPause invoked&#8221;);<\/p>\n<p>}<\/p>\n<p>@Override<br \/>\nprotected void onStop()<br \/>\n{<br \/>\nsuper.onStop();<br \/>\ntv5.setText(&#8220;onStop invoked&#8221;);<br \/>\nLog.d(&#8220;Lifecycle&#8221;,&#8221;onStop invoked&#8221;);<\/p>\n<p>}<\/p>\n<p>@Override<br \/>\nprotected void onRestart()<br \/>\n{<br \/>\nsuper.onRestart();<br \/>\ntv6.setText(&#8220;onRestart invoked&#8221;);<br \/>\nLog.d(&#8220;Lifecycle&#8221;,&#8221;onRestart invoked&#8221;);<\/p>\n<p>}<\/p>\n<p>@Override<br \/>\nprotected void onDestroy()<br \/>\n{<br \/>\nsuper.onDestroy();<br \/>\ntv7.setText(&#8220;onDestroy invoked&#8221;);<br \/>\nLog.d(&#8220;Lifecycle&#8221;,&#8221;onDestroy invoked&#8221;);<\/p>\n<p>}<\/p>\n<p>@Override<br \/>\npublic boolean onCreateOptionsMenu(Menu menu) {<br \/>\n\/\/ Inflate the menu; this adds items to the action bar if it is present.<br \/>\ngetMenuInflater().inflate(R.menu.main, menu);<br \/>\nreturn true;<br \/>\n}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/youtu.be\/kB3O8WFN14k\">https:\/\/youtu.be\/kB3O8WFN14k<\/a><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"In this tutorial Android Activity Life Cycle \u00a0methods are shown. There are mainly 4 states of Activity, they are as below: Running: Activity is visible and interacts with the user Paused: Activity is still visible but partially obscured, instance is running but might be killed by the system Stopped: Activity is not visible, instance is running but might be killed by \u00a0the system Killed: Activity has been terminated by the system or by a call to its finish() method There are 7 methods of Activity Life Cycle they are as below: onCreate(): It is called once when your activity is created onStart(): It is called after onCreate() when your activity is started onPause(): It is called when you minimise your <!-- AddThis Advanced Settings generic via filter on get_the_excerpt -->","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62],"tags":[70],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2419"}],"collection":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/comments?post=2419"}],"version-history":[{"count":7,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2419\/revisions"}],"predecessor-version":[{"id":2429,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2419\/revisions\/2429"}],"wp:attachment":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/media?parent=2419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/categories?post=2419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/tags?post=2419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}