{"id":8,"date":"2018-02-07T08:44:09","date_gmt":"2018-02-07T08:44:09","guid":{"rendered":"https:\/\/nezis.gr\/?p=8"},"modified":"2021-12-09T07:44:00","modified_gmt":"2021-12-09T07:44:00","slug":"android-activity-lifecycle-2-methods","status":"publish","type":"post","link":"https:\/\/nezis.gr\/?p=8","title":{"rendered":"Android Activity-lifecycle-2 (methods)"},"content":{"rendered":"<h2 id=\"lc\"><img decoding=\"async\" class=\"transparent\" src=\"https:\/\/i.stack.imgur.com\/kChdb.png\" alt=\"https:\/\/i.stack.imgur.com\/kChdb.png\"><\/h2>\n<p><!--more--><\/p>\n<hr>\n<blockquote>\n<h3 id=\"oncreate\">onCreate()<\/h3>\n<\/blockquote>\n<p>You must implement this callback, which fires when the system first creates the activity. On activity creation, the activity enters the <em>Created<\/em> state. In the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onCreate(android.os.Bundle)\">onCreate()<\/a><\/code> method, you perform basic application startup logic that should happen only once for the entire life of the activity. For example, your implementation of <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onCreate(android.os.Bundle)\">onCreate()<\/a><\/code> might bind data to lists, initialize background threads, and instantiate some class-scope variables. This method receives the parameter <code>savedInstanceState<\/code>, which is a <code><a href=\"https:\/\/developer.android.com\/reference\/android\/os\/Bundle.html\">Bundle<\/a><\/code> object containing the activity&#8217;s previously saved state. If the activity has never existed before, the value of the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/os\/Bundle.html\">Bundle<\/a><\/code> object is null.<\/p>\n<p>The following example of the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onCreate(android.os.Bundle)\">onCreate()<\/a><\/code> method shows fundamental setup for the activity, such as declaring the user interface (defined in an XML layout file), defining member variables, and configuring some of the UI. In this example, the XML layout file is specified by passing file\u2019s resource ID <code>R.layout.main_activity<\/code> to <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#setContentView(android.view.View)\">setContentView()<\/a><\/code>.<\/p>\n<pre class=\"prettyprint\"><span class=\"typ\">TextView<\/span><span class=\"pln\"> mTextView<\/span><span class=\"pun\">;<\/span>\n\n<span class=\"com\">\/\/ some transient state for the activity instance<\/span>\n<span class=\"typ\">String<\/span><span class=\"pln\"> mGameState<\/span><span class=\"pun\">;<\/span>\n\n<span class=\"lit\">@Override<\/span>\n<span class=\"kwd\">public<\/span> <span class=\"kwd\">void<\/span><span class=\"pln\"> onCreate<\/span><span class=\"pun\">(<\/span><span class=\"typ\">Bundle<\/span><span class=\"pln\"> savedInstanceState<\/span><span class=\"pun\">)<\/span> <span class=\"pun\">{<\/span><span class=\"pln\">\n&nbsp; &nbsp; <\/span><span class=\"com\">\/\/ call the super class onCreate to complete the creation of activity like<\/span><span class=\"pln\">\n&nbsp; &nbsp; <\/span><span class=\"com\">\/\/ the view hierarchy<\/span><span class=\"pln\">\n&nbsp; &nbsp; <\/span><span class=\"kwd\">super<\/span><span class=\"pun\">.<\/span><span class=\"pln\">onCreate<\/span><span class=\"pun\">(<\/span><span class=\"pln\">savedInstanceState<\/span><span class=\"pun\">);<\/span><span class=\"pln\">\n\n&nbsp; &nbsp; <\/span><span class=\"com\">\/\/ recovering the instance state<\/span><span class=\"pln\">\n&nbsp; &nbsp; <\/span><span class=\"kwd\">if<\/span> <span class=\"pun\">(<\/span><span class=\"pln\">savedInstanceState <\/span><span class=\"pun\">!=<\/span> <span class=\"kwd\">null<\/span><span class=\"pun\">)<\/span> <span class=\"pun\">{<\/span><span class=\"pln\">\n&nbsp; &nbsp; &nbsp; &nbsp; mGameState <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> savedInstanceState<\/span><span class=\"pun\">.<\/span><span class=\"pln\">getString<\/span><span class=\"pun\">(<\/span><span class=\"pln\">GAME_STATE_KEY<\/span><span class=\"pun\">);<\/span><span class=\"pln\">\n&nbsp; &nbsp; <\/span><span class=\"pun\">}<\/span><span class=\"pln\">\n\n&nbsp; &nbsp; <\/span><span class=\"com\">\/\/ set the user interface layout for this Activity<\/span><span class=\"pln\">\n&nbsp; &nbsp; <\/span><span class=\"com\">\/\/ the layout file is defined in the project res\/layout\/main_activity.xml file<\/span><span class=\"pln\">\n&nbsp; &nbsp; setContentView<\/span><span class=\"pun\">(<\/span><span class=\"pln\">R<\/span><span class=\"pun\">.<\/span><span class=\"pln\">layout<\/span><span class=\"pun\">.<\/span><span class=\"pln\">main_activity<\/span><span class=\"pun\">);<\/span><span class=\"pln\">\n\n&nbsp; &nbsp; <\/span><span class=\"com\">\/\/ initialize member TextView so we can manipulate it later<\/span><span class=\"pln\">\n&nbsp; &nbsp; mTextView <\/span><span class=\"pun\">=<\/span> <span class=\"pun\">(<\/span><span class=\"typ\">TextView<\/span><span class=\"pun\">)<\/span><span class=\"pln\"> findViewById<\/span><span class=\"pun\">(<\/span><span class=\"pln\">R<\/span><span class=\"pun\">.<\/span><span class=\"pln\">id<\/span><span class=\"pun\">.<\/span><span class=\"pln\">text_view<\/span><span class=\"pun\">);<\/span>\n<span class=\"pun\">}<\/span>\n\n<span class=\"com\">\/\/ This callback is called only when there is a saved instance previously saved using<\/span>\n<span class=\"com\">\/\/ onSaveInstanceState(). We restore some state in onCreate() while we can optionally restore<\/span>\n<span class=\"com\">\/\/ other state here, possibly usable after onStart() has completed.<\/span>\n<span class=\"com\">\/\/ The savedInstanceState Bundle is same as the one used in onCreate().<\/span>\n<span class=\"lit\">@Override<\/span>\n<span class=\"kwd\">public<\/span> <span class=\"kwd\">void<\/span><span class=\"pln\"> onRestoreInstanceState<\/span><span class=\"pun\">(<\/span><span class=\"typ\">Bundle<\/span><span class=\"pln\"> savedInstanceState<\/span><span class=\"pun\">)<\/span> <span class=\"pun\">{<\/span><span class=\"pln\">\n&nbsp; &nbsp; mTextView<\/span><span class=\"pun\">.<\/span><span class=\"pln\">setText<\/span><span class=\"pun\">(<\/span><span class=\"pln\">savedInstanceState<\/span><span class=\"pun\">.<\/span><span class=\"pln\">getString<\/span><span class=\"pun\">(<\/span><span class=\"pln\">TEXT_VIEW_KEY<\/span><span class=\"pun\">));<\/span>\n<span class=\"pun\">}<\/span>\n\n<span class=\"com\">\/\/ invoked when the activity may be temporarily destroyed, save the instance state here<\/span>\n<span class=\"lit\">@Override<\/span>\n<span class=\"kwd\">public<\/span> <span class=\"kwd\">void<\/span><span class=\"pln\"> onSaveInstanceState<\/span><span class=\"pun\">(<\/span><span class=\"typ\">Bundle<\/span><span class=\"pln\"> outState<\/span><span class=\"pun\">)<\/span> <span class=\"pun\">{<\/span><span class=\"pln\">\n&nbsp; &nbsp; outState<\/span><span class=\"pun\">.<\/span><span class=\"pln\">putString<\/span><span class=\"pun\">(<\/span><span class=\"pln\">GAME_STATE_KEY<\/span><span class=\"pun\">,<\/span><span class=\"pln\"> mGameState<\/span><span class=\"pun\">);<\/span><span class=\"pln\">\n&nbsp; &nbsp; outState<\/span><span class=\"pun\">.<\/span><span class=\"pln\">putString<\/span><span class=\"pun\">(<\/span><span class=\"pln\">TEXT_VIEW_KEY<\/span><span class=\"pun\">,<\/span><span class=\"pln\"> mTextView<\/span><span class=\"pun\">.<\/span><span class=\"pln\">getText<\/span><span class=\"pun\">());<\/span><span class=\"pln\">\n\n&nbsp; &nbsp; <\/span><span class=\"com\">\/\/ call superclass to save any view hierarchy<\/span><span class=\"pln\">\n&nbsp; &nbsp; <\/span><span class=\"kwd\">super<\/span><span class=\"pun\">.<\/span><span class=\"pln\">onSaveInstanceState<\/span><span class=\"pun\">(<\/span><span class=\"pln\">outState<\/span><span class=\"pun\">);<\/span>\n<span class=\"pun\">}<\/span><\/pre>\n<p>As an alternative to defining the XML file and passing it to <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#setContentView(android.view.View)\">setContentView()<\/a><\/code>, you can create new <code><a href=\"https:\/\/developer.android.com\/reference\/android\/view\/View.html\">View<\/a><\/code> objects in your activity code and build a view hierarchy by inserting new <code><a href=\"https:\/\/developer.android.com\/reference\/android\/view\/View.html\">View<\/a><\/code>s into a <code><a href=\"https:\/\/developer.android.com\/reference\/android\/view\/ViewGroup.html\">ViewGroup<\/a><\/code>. You then use that layout by passing the root <code><a href=\"https:\/\/developer.android.com\/reference\/android\/view\/ViewGroup.html\">ViewGroup<\/a><\/code> to <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#setContentView(android.view.View)\">setContentView()<\/a><\/code>. For more information about creating a user interface, see the <a href=\"https:\/\/developer.android.com\/guide\/topics\/ui\/index.html\">User Interface<\/a> documentation.<\/p>\n<p>Your activity does not reside in the Created state. After the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onCreate(android.os.Bundle)\">onCreate()<\/a><\/code> method finishes execution, the activity enters the <em>Started<\/em> state, and the system calls the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStart()\">onStart()<\/a><\/code> and <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onResume()\">onResume()<\/a><\/code> methods in quick succession. The next section explains the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStart()\">onStart()<\/a><\/code> callback.<\/p>\n<h2 id=\"lc\"><img decoding=\"async\" class=\"transparent\" src=\"https:\/\/i.stack.imgur.com\/kChdb.png\" alt=\"https:\/\/i.stack.imgur.com\/kChdb.png\"><\/h2>\n<blockquote>\n<h3 id=\"onstart\">onStart()<\/h3>\n<\/blockquote>\n<p>When the activity enters the Started state, the system invokes this callback. The <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStart()\">onStart()<\/a><\/code> call makes the activity visible to the user, as the app prepares for the activity to enter the foreground and become interactive. For example, this method is where the app initializes the code that maintains the UI. It might also register a <code><a href=\"https:\/\/developer.android.com\/reference\/android\/content\/BroadcastReceiver.html\">BroadcastReceiver<\/a><\/code> that monitors changes that are reflected in the UI.<\/p>\n<p>The <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStart()\">onStart()<\/a><\/code> method completes very quickly and, as with the Created state, the activity does not stay resident in the Started state. Once this callback finishes, the activity enters the <em>Resumed<\/em> state, and the system invokes the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onResume()\">onResume()<\/a><\/code> method.<\/p>\n<h2 id=\"lc\"><img decoding=\"async\" class=\"transparent\" src=\"https:\/\/i.stack.imgur.com\/kChdb.png\" alt=\"https:\/\/i.stack.imgur.com\/kChdb.png\"><\/h2>\n<blockquote>\n<h3 id=\"onresume\">onResume()<\/h3>\n<\/blockquote>\n<p>When the activity enters the Resumed state, it comes to the foreground, and then the system invokes the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onResume()\">onResume()<\/a><\/code> callback. This is the state in which the app interacts with the user. The app stays in this state until something happens to take focus away from the app. Such an event might be, for instance, receiving a phone call, the user\u2019s navigating to another activity, or the device screen\u2019s turning off.<\/p>\n<p>When an interruptive event occurs, the activity enters the <em>Paused<\/em> state, and the system invokes the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onPause()\">onPause()<\/a><\/code> callback.<\/p>\n<p>If the activity returns to the Resumed state from the Paused state, the system once again calls <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onResume()\">onResume()<\/a><\/code> method. For this reason, you should implement <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onResume()\">onResume()<\/a><\/code> to initialize components that you release during <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onPause()\">onPause()<\/a><\/code>. For example, you may initialize the camera as follows:<\/p>\n<pre class=\"prettyprint\"><span class=\"lit\">@Override<\/span>\n<span class=\"kwd\">public<\/span> <span class=\"kwd\">void<\/span><span class=\"pln\"> onResume<\/span><span class=\"pun\">()<\/span> <span class=\"pun\">{<\/span><span class=\"pln\">\n&nbsp; &nbsp; <\/span><span class=\"kwd\">super<\/span><span class=\"pun\">.<\/span><span class=\"pln\">onResume<\/span><span class=\"pun\">();<\/span><span class=\"pln\"> &nbsp;<\/span><span class=\"com\">\/\/ Always call the superclass method first<\/span><span class=\"pln\">\n\n\n&nbsp; &nbsp; <\/span><span class=\"com\">\/\/ Get the Camera instance as the activity achieves full user focus<\/span><span class=\"pln\">\n&nbsp; &nbsp; <\/span><span class=\"kwd\">if<\/span> <span class=\"pun\">(<\/span><span class=\"pln\">mCamera <\/span><span class=\"pun\">==<\/span> <span class=\"kwd\">null<\/span><span class=\"pun\">)<\/span> <span class=\"pun\">{<\/span><span class=\"pln\">\n&nbsp; &nbsp; &nbsp; &nbsp; initializeCamera<\/span><span class=\"pun\">();<\/span> <span class=\"com\">\/\/ Local method to handle camera init<\/span><span class=\"pln\">\n&nbsp; &nbsp; <\/span><span class=\"pun\">}<\/span>\n<span class=\"pun\">}<\/span><span class=\"pln\">\n&nbsp; &nbsp;<\/span><\/pre>\n<p>Be aware that the system calls this method every time your activity comes into the foreground, including when it&#8217;s created for the first time. As such, you should implement <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onResume()\">onResume()<\/a><\/code> to initialize components that you release during <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onPause()\">onPause()<\/a><\/code>, and perform any other initializations that must occur each time the activity enters the Resumed state. For example, you should begin animations and initialize components that the activity only uses when it has user focus.<\/p>\n<h2 id=\"lc\"><img decoding=\"async\" class=\"transparent\" src=\"https:\/\/i.stack.imgur.com\/kChdb.png\" alt=\"https:\/\/i.stack.imgur.com\/kChdb.png\"><\/h2>\n<blockquote>\n<h3 id=\"onpause\">onPause()<\/h3>\n<\/blockquote>\n<p>The system calls this method as the first indication that the user is leaving your activity (though it does not always mean the activity is being destroyed). Use the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onPause()\">onPause()<\/a><\/code> method to pause operations such animations and music playback that should not continue while the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html\">Activity<\/a><\/code> is in the Paused state, and that you expect to resume shortly. There are several reasons why an activity may enter this state. For example:<\/p>\n<ul>\n<li>Some event interrupts app execution, as described in the <a href=\"https:\/\/developer.android.com\/guide\/components\/activities\/activity-lifecycle.html#onresume\">onResume()<\/a> section. This is the most common case.<\/li>\n<li>In Android 7.0 (API level 24) or higher, multiple apps run in multi-window mode. Because only one of the apps (windows) has focus at any time, the system pauses all of the other apps.<\/li>\n<li>A new, semi-transparent activity (such as a dialog) opens. As long as the activity is still partially visible but not in focus, it remains paused.<\/li>\n<\/ul>\n<p>You can use the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onPause()\">onPause()<\/a><\/code> method to release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them.<\/p>\n<p>For example, if your application uses the Camera, the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onPause()\">onPause()<\/a><\/code> method is a good place to release it. The following example of <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onPause()\">onPause()<\/a><\/code> is the counterpart to the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onResume()\">onResume()<\/a><\/code> example above, releasing the camera that the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onResume()\">onResume()<\/a><\/code> example initialized.<\/p>\n<pre class=\"prettyprint\"><span class=\"lit\">@Override<\/span>\n<span class=\"kwd\">public<\/span> <span class=\"kwd\">void<\/span><span class=\"pln\"> onPause<\/span><span class=\"pun\">()<\/span> <span class=\"pun\">{<\/span><span class=\"pln\">\n&nbsp; &nbsp; <\/span><span class=\"kwd\">super<\/span><span class=\"pun\">.<\/span><span class=\"pln\">onPause<\/span><span class=\"pun\">();<\/span><span class=\"pln\"> &nbsp;<\/span><span class=\"com\">\/\/ Always call the superclass method first<\/span><span class=\"pln\">\n\n\n&nbsp; &nbsp; <\/span><span class=\"com\">\/\/ Release the Camera because we don't need it when paused<\/span><span class=\"pln\">\n&nbsp; &nbsp; <\/span><span class=\"com\">\/\/ and other activities might need to use it.<\/span><span class=\"pln\">\n&nbsp; &nbsp; <\/span><span class=\"kwd\">if<\/span> <span class=\"pun\">(<\/span><span class=\"pln\">mCamera <\/span><span class=\"pun\">!=<\/span> <span class=\"kwd\">null<\/span><span class=\"pun\">)<\/span> <span class=\"pun\">{<\/span><span class=\"pln\">\n&nbsp; &nbsp; &nbsp; &nbsp; mCamera<\/span><span class=\"pun\">.<\/span><span class=\"pln\">release<\/span><span class=\"pun\">();<\/span><span class=\"pln\">\n&nbsp; &nbsp; &nbsp; &nbsp; mCamera <\/span><span class=\"pun\">=<\/span> <span class=\"kwd\">null<\/span><span class=\"pun\">;<\/span><span class=\"pln\">\n&nbsp; &nbsp; <\/span><span class=\"pun\">}<\/span>\n<span class=\"pun\">}<\/span><\/pre>\n<p><code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onPause()\">onPause()<\/a><\/code> execution is very brief, and does not necessarily afford enough time to perform save operations. For this reason, you should <strong>not<\/strong> use <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onPause()\">onPause()<\/a><\/code> to save application or user data, make network calls, or execute database transactions; such work may not complete before the method completes. Instead, you should perform heavy-load shutdown operations during <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStop()\">onStop()<\/a><\/code>. For more information about suitable operations to perform during <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStop()\">onStop()<\/a><\/code>, see <a href=\"https:\/\/developer.android.com\/guide\/components\/activities\/activity-lifecycle.html#onstop\"> onStop()<\/a>. For more information about saving data, see <a href=\"https:\/\/developer.android.com\/guide\/components\/activities\/activity-lifecycle.html#saras\">Saving and restoring activity state<\/a>.<\/p>\n<p>Completion of the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onPause()\">onPause()<\/a><\/code> method does not mean that the activity leaves the Paused state. Rather, the activity remains in this state until either the activity resumes or becomes completely invisible to the user. If the activity resumes, the system once again invokes the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onResume()\">onResume()<\/a><\/code> callback. If the activity returns from the Paused state to the Resumed state, the system keeps the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html\">Activity<\/a><\/code> instance resident in memory, recalling that instance when it the system invokes <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onResume()\">onResume()<\/a><\/code>. In this scenario, you don\u2019t need to re-initialize components that were created during any of the callback methods leading up to the Resumed state. If the activity becomes completely invisible, the system calls <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStop()\">onStop()<\/a><\/code>. The next section discusses the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStop()\">onStop()<\/a><\/code> callback.<\/p>\n<h2 id=\"lc\"><img decoding=\"async\" class=\"transparent\" src=\"https:\/\/i.stack.imgur.com\/kChdb.png\" alt=\"https:\/\/i.stack.imgur.com\/kChdb.png\"><\/h2>\n<blockquote>\n<h3 id=\"onstop\">onStop()<\/h3>\n<\/blockquote>\n<p>When your activity is no longer visible to the user, it has entered the <em>Stopped<\/em> state, and the system invokes the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStop()\">onStop()<\/a><\/code> callback. This may occur, for example, when a newly launched activity covers the entire screen. The system may also call <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStop()\">onStop()<\/a><\/code> when the activity has finished running, and is about to be terminated.<\/p>\n<p>In the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStop()\">onStop()<\/a><\/code> method, the app should release almost all resources that aren&#8217;t needed while the user is not using it. For example, if you registered a <code><a href=\"https:\/\/developer.android.com\/reference\/android\/content\/BroadcastReceiver.html\">BroadcastReceiver<\/a><\/code> in <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStart()\">onStart()<\/a><\/code> to listen for changes that might affect your UI, you can unregister the broadcast receiver in <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStop()\">onStop()<\/a><\/code>, as the user can no longer see the UI. It is also important that you use <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStop()\">onStop()<\/a><\/code> to release resources that might leak memory, because it is possible for the system to kill the process hosting your activity without calling the activity&#8217;s final <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onDestroy()\">onDestroy()<\/a><\/code> callback.<\/p>\n<p>You should also use <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStop()\">onStop()<\/a><\/code> to perform relatively CPU-intensive shutdown operations. For example, if you can&#8217;t find a more opportune time to save information to a database, you might do so during <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStop()\">onStop()<\/a><\/code>. The following example shows an implementation of <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStop()\">onStop()<\/a><\/code> that saves the contents of a draft note to persistent storage:<\/p>\n<pre class=\"prettyprint\"><span class=\"lit\">@Override<\/span>\n<span class=\"kwd\">protected<\/span> <span class=\"kwd\">void<\/span><span class=\"pln\"> onStop<\/span><span class=\"pun\">()<\/span> <span class=\"pun\">{<\/span><span class=\"pln\">\n&nbsp; &nbsp; <\/span><span class=\"com\">\/\/ call the superclass method first<\/span><span class=\"pln\">\n&nbsp; &nbsp; <\/span><span class=\"kwd\">super<\/span><span class=\"pun\">.<\/span><span class=\"pln\">onStop<\/span><span class=\"pun\">();<\/span><span class=\"pln\">\n\n&nbsp; &nbsp; <\/span><span class=\"com\">\/\/ save the note's current draft, because the activity is stopping<\/span><span class=\"pln\">\n&nbsp; &nbsp; <\/span><span class=\"com\">\/\/ and we want to be sure the current note progress isn't lost.<\/span><span class=\"pln\">\n&nbsp; &nbsp; <\/span><span class=\"typ\">ContentValues<\/span><span class=\"pln\"> values <\/span><span class=\"pun\">=<\/span> <span class=\"kwd\">new<\/span> <span class=\"typ\">ContentValues<\/span><span class=\"pun\">();<\/span><span class=\"pln\">\n&nbsp; &nbsp; values<\/span><span class=\"pun\">.<\/span><span class=\"pln\">put<\/span><span class=\"pun\">(<\/span><span class=\"typ\">NotePad<\/span><span class=\"pun\">.<\/span><span class=\"typ\">Notes<\/span><span class=\"pun\">.<\/span><span class=\"pln\">COLUMN_NAME_NOTE<\/span><span class=\"pun\">,<\/span><span class=\"pln\"> getCurrentNoteText<\/span><span class=\"pun\">());<\/span><span class=\"pln\">\n&nbsp; &nbsp; values<\/span><span class=\"pun\">.<\/span><span class=\"pln\">put<\/span><span class=\"pun\">(<\/span><span class=\"typ\">NotePad<\/span><span class=\"pun\">.<\/span><span class=\"typ\">Notes<\/span><span class=\"pun\">.<\/span><span class=\"pln\">COLUMN_NAME_TITLE<\/span><span class=\"pun\">,<\/span><span class=\"pln\"> getCurrentNoteTitle<\/span><span class=\"pun\">());<\/span><span class=\"pln\">\n\n&nbsp; &nbsp; <\/span><span class=\"com\">\/\/ do this update in background on an AsyncQueryHandler or equivalent<\/span><span class=\"pln\">\n&nbsp; &nbsp; mAsyncQueryHandler<\/span><span class=\"pun\">.<\/span><span class=\"pln\">startUpdate <\/span><span class=\"pun\">(<\/span><span class=\"pln\">\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mToken<\/span><span class=\"pun\">,<\/span><span class=\"pln\"> &nbsp;<\/span><span class=\"com\">\/\/ int token to correlate calls<\/span><span class=\"pln\">\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"kwd\">null<\/span><span class=\"pun\">,<\/span><span class=\"pln\"> &nbsp; &nbsp;<\/span><span class=\"com\">\/\/ cookie, not used here<\/span><span class=\"pln\">\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mUri<\/span><span class=\"pun\">,<\/span><span class=\"pln\"> &nbsp; &nbsp;<\/span><span class=\"com\">\/\/ The URI for the note to update.<\/span><span class=\"pln\">\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; values<\/span><span class=\"pun\">,<\/span><span class=\"pln\"> &nbsp;<\/span><span class=\"com\">\/\/ The map of column names and new values to apply to them.<\/span><span class=\"pln\">\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"kwd\">null<\/span><span class=\"pun\">,<\/span><span class=\"pln\"> &nbsp; &nbsp;<\/span><span class=\"com\">\/\/ No SELECT criteria are used.<\/span><span class=\"pln\">\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <\/span><span class=\"kwd\">null<\/span><span class=\"pln\"> &nbsp; &nbsp; <\/span><span class=\"com\">\/\/ No WHERE columns are used.<\/span><span class=\"pln\">\n&nbsp; &nbsp; <\/span><span class=\"pun\">);<\/span>\n<span class=\"pun\">}<\/span><\/pre>\n<p>When your activity enters the Stopped state, the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html\">Activity<\/a><\/code> object is kept resident in memory: It maintains all state and member information, but is not attached to the window manager. When the activity resumes, the activity recalls this information. You don\u2019t need to re-initialize components that were created during any of the callback methods leading up to the Resumed state. The system also keeps track of the current state for each <code><a href=\"https:\/\/developer.android.com\/reference\/android\/view\/View.html\">View<\/a><\/code> object in the layout, so if the user entered text into an <code><a href=\"https:\/\/developer.android.com\/reference\/android\/widget\/EditText.html\">EditText<\/a><\/code> widget, that content is retained so you don&#8217;t need to save and restore it.<\/p>\n<p class=\"note\"><strong>Note: <\/strong>Once your activity is stopped, the system might destroy the process that contains the activity if the system needs to recovery memory. Even if the system destroys the process while the activity is stopped, the system still retains the state of the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/view\/View.html\">View<\/a><\/code> objects (such as text in an <code><a href=\"https:\/\/developer.android.com\/reference\/android\/widget\/EditText.html\">EditText<\/a><\/code> widget) in a <code><a href=\"https:\/\/developer.android.com\/reference\/android\/os\/Bundle.html\">Bundle<\/a><\/code> (a blob of key-value pairs) and restores them if the user navigates back to the activity. For more information about restoring an activity to which a user returns, see <a href=\"https:\/\/developer.android.com\/guide\/components\/activities\/activity-lifecycle.html#saras\">Saving and restoring activity state<\/a>.<\/p>\n<p>From the Stopped state, the activity either comes back to interact with the user, or the activity is finished running and goes away. If the activity comes back, the system invokes <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onRestart()\">onRestart()<\/a><\/code>. If the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html\">Activity<\/a><\/code> is finished running, the system calls <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onDestroy()\">onDestroy()<\/a><\/code>. The next section explains the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onDestroy()\">onDestroy()<\/a><\/code> callback.<\/p>\n<h2 id=\"lc\"><img decoding=\"async\" class=\"transparent\" src=\"https:\/\/i.stack.imgur.com\/kChdb.png\" alt=\"https:\/\/i.stack.imgur.com\/kChdb.png\"><\/h2>\n<blockquote>\n<h3 id=\"ondestroy\">onDestroy()<\/h3>\n<\/blockquote>\n<p>Called before the activity is destroyed. This is the final call that the activity receives. The system either invokes this callback because the activity is finishing due to someone&#8217;s calling <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#finish()\">finish()<\/a><\/code>, or because the system is temporarily destroying the process containing the activity to save space. You can distinguish between these two scenarios with the <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#isFinishing()\">isFinishing()<\/a><\/code> method. The system may also call this method when an orientation change occurs, and then immediately call <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onCreate(android.os.Bundle)\">onCreate()<\/a><\/code> to recreate the process (and the components that it contains) in the new orientation.<\/p>\n<p>The <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onDestroy()\">onDestroy()<\/a><\/code> callback releases all resources that have not yet been released by earlier callbacks such as <code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStop()\">onStop()<\/a><\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":72,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[2],"tags":[3,4],"class_list":["post-8","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","tag-android","tag-android-studio"],"_links":{"self":[{"href":"https:\/\/nezis.gr\/index.php?rest_route=\/wp\/v2\/posts\/8","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nezis.gr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nezis.gr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nezis.gr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nezis.gr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8"}],"version-history":[{"count":5,"href":"https:\/\/nezis.gr\/index.php?rest_route=\/wp\/v2\/posts\/8\/revisions"}],"predecessor-version":[{"id":90,"href":"https:\/\/nezis.gr\/index.php?rest_route=\/wp\/v2\/posts\/8\/revisions\/90"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nezis.gr\/index.php?rest_route=\/wp\/v2\/media\/72"}],"wp:attachment":[{"href":"https:\/\/nezis.gr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nezis.gr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nezis.gr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}