{"id":419,"date":"2015-12-29T07:49:07","date_gmt":"2015-12-29T07:49:07","guid":{"rendered":"http:\/\/abhiandroid.com\/programming\/?p=419"},"modified":"2018-06-06T04:52:42","modified_gmt":"2018-06-06T04:52:42","slug":"onstop-tutorial-example","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/programming\/onstop-tutorial-example.html","title":{"rendered":"onStop() Method Tutorial With Example In Android Studio"},"content":{"rendered":"<h4><strong>onStop() Method In Android Activity Life Cycle<\/strong><\/h4>\n<ul>\n<li>When Activity is in background then onPause() method will execute. After a millisecond of that method next onStop() method will execute.<\/li>\n<li>Means here also Activity is not visible to user when onStop() executed.<\/li>\n<li>We will use onStop() method to stop Api calls etc. This onStop() method will<br \/>\nclean up all your activities resources.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>onStop() Explanation With Example In Android Studio:<\/strong><\/h4>\n<p>Now we will create an example in which you see how onStop() method call in Android. In this example we will use toast message to display the message when this method is called.<\/p>\n<p><strong>Below is the final output of the example that we will create:<\/strong><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-428\" src=\"\/programming\/wp-content\/uploads\/2015\/12\/onStop-Example-final-output-screen.jpg\" alt=\"onStop Example final output screen\" width=\"353\" height=\"641\" srcset=\"https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/onStop-Example-final-output-screen.jpg 353w, https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/onStop-Example-final-output-screen-165x300.jpg 165w\" sizes=\"auto, (max-width: 353px) 100vw, 353px\" \/><\/center><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project Onstopexample and create an Activity name Main Activity. You will notice content_main.xml will be created by default in Android Studio. Now design a simple button &#8220;Next Activity&#8221; and text asking user to click on the button.<\/p>\n<p>Below is the code of content_main.xml:<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:orientation=\"vertical\" android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:id=\"@+id\/layout\"\r\n    android:background=\"#000\"&gt;\r\n\r\n    &lt;Button\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:onClick=\"onClickButton\"\r\n        android:text=\"Next Activity\"\r\n        android:id=\"@+id\/button\" \/&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\/textAppearanceLarge\"\r\n        android:text=\"Click on Next Activity Button to see onStop() called after onPause() for this activity\"\r\n        android:id=\"@+id\/textView\"\r\n        android:layout_below=\"@+id\/button\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:textColor=\"#ffffff\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Now we will code MainActivity.java where we will use Intent to move on the Next Activity when Button is clicked. The Next Activity will be created in next step. Also we will override methods of Activity Life Cycle and use Toast message to display which method is called.<\/p>\n<p>Below is the code of MainActivity.java<\/p>\n<pre>package abhiandroid.com.onstopexample;\r\n\r\nimport android.content.Intent;\r\nimport 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.view.View;\r\nimport android.view.Menu;\r\nimport android.view.MenuItem;\r\nimport android.widget.RelativeLayout;\r\nimport android.widget.Toast;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    RelativeLayout layout;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.content_main);\r\n        layout = (RelativeLayout) findViewById(R.id.layout);\r\n\r\n    }\r\n\r\n    public void onClickButton(View v) {\r\n        Intent i = new Intent(this, NextActivity.class);\r\n        startActivity(i);\r\n\r\n    }\r\n\r\n    @Override\r\n    protected void onStart() {\r\n        super.onStart();\r\n        Toast.makeText(getApplicationContext(), \"onStart called\", Toast.LENGTH_LONG).show();\r\n    }\r\n\r\n    @Override\r\n    protected void onResume() {\r\n        super.onResume();\r\n        Toast.makeText(getApplicationContext(), \"onResumed called\", Toast.LENGTH_LONG).show();\r\n    }\r\n    @Override\r\n    protected void onPause() {\r\n        super.onResume();\r\n        Toast.makeText(getApplicationContext(), \"onPause called\", Toast.LENGTH_LONG).show();\r\n    }\r\n    @Override\r\n    protected void onStop() {\r\n        super.onStop();  \/\/ Always call the superclass method first\r\n        Toast.makeText(getApplicationContext(), \"onStop called\", Toast.LENGTH_LONG).show();\r\n    }\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Now we will simply create a Next Activity content_next.xml. Here we will display the text onStop() method called for previous activity.<\/p>\n<p>Below is the code of content_next.xml<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:orientation=\"vertical\" android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_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\/textAppearanceLarge\"\r\n        android:text=\"onStop() called after onPause() for previous activity\"\r\n        android:id=\"@+id\/textView2\"\r\n        android:layout_alignParentTop=\"true\"\r\n        android:layout_alignParentLeft=\"true\"\r\n        android:layout_alignParentStart=\"true\" \/&gt;\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span> The NextActivity.java will simply call the layout content_next.xml.<\/p>\n<p>Below is the code of\u00a0NextActivity.java<\/p>\n<pre>package abhiandroid.com.onstopexample;\r\n\r\nimport 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.view.View;\r\n\r\npublic class NextActivity extends AppCompatActivity {\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.content_next);\r\n\r\n    }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 5:<\/strong><\/span> Now make sure your Manifest file has both the Activity listed in it. Below is the AndroidManifest.xml code:<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;manifest xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    package=\"abhiandroid.com.onstopexample\"&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;activity\r\n            android:name=\".NextActivity\"\r\n            android:label=\"@string\/title_activity_next\"\r\n            android:theme=\"@style\/AppTheme.NoActionBar\"&gt;&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><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Now run the App in AVD, you will see the below output screen<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-429\" src=\"\/programming\/wp-content\/uploads\/2015\/12\/onStop-Example-Output-main-screen.jpg\" alt=\"onStop Example Output main screen\" width=\"351\" height=\"197\" srcset=\"https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/onStop-Example-Output-main-screen.jpg 351w, https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/onStop-Example-Output-main-screen-300x168.jpg 300w\" sizes=\"auto, (max-width: 351px) 100vw, 351px\" \/><\/center><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Click on the &#8220;Next Activity&#8221; button and you will see onStop() method will be called for previous activity after onPause() because that activity goes in background.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-430\" src=\"\/programming\/wp-content\/uploads\/2015\/12\/onStop-method-called-after-onPause.jpg\" alt=\"onStop() method called after onPause()\" width=\"356\" height=\"499\" srcset=\"https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/onStop-method-called-after-onPause.jpg 356w, https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/onStop-method-called-after-onPause-214x300.jpg 214w\" sizes=\"auto, (max-width: 356px) 100vw, 356px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Summary:<\/strong><\/h4>\n<p>onStop() method is called after onPause() method when activity goes in background. This method can be used to stop Api calls etc.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>onStop() Method In Android Activity Life Cycle When Activity is in background then onPause() method will execute. After a millisecond of that method next onStop() method will execute. Means here also Activity is not visible to user when onStop() executed. We will use onStop() method to stop Api calls etc. This onStop() method will clean &hellip; <a href=\"https:\/\/abhiandroid.com\/programming\/onstop-tutorial-example.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">onStop() Method Tutorial With Example In Android Studio<\/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-419","post","type-post","status-publish","format-standard","hentry","category-android-methods","category-archieve"],"psp_head":"<title>onStop() Method Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on onStop method in Activity Life Cycle with example and code in Android Studio. Activity is not visible to user when onStop() executed.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/programming\/onstop-tutorial-example.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/posts\/419","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=419"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/posts\/419\/revisions"}],"predecessor-version":[{"id":914,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/posts\/419\/revisions\/914"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/media?parent=419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/categories?post=419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/tags?post=419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}