Managing the Activity Lifecycle --- Stopping and Restarting

Source: Internet
Author: User

Specified Properly stopping and restarting your activity is an important process in the activity lifecycle that ensures your users perceive that your app is always alive and doesn't lose their progress. there are a few of key scenarios in which your activity is stopped and restarted: The user Opens the Recent Apps window and switches from your app to another app. the activity in your app that's currently in the foreground is stopped. if the user returns to your app from the Home screen launcher icon or the Recent Apps window, the activity restarts. the user performs an action in your app that starts a new activity. the current activity is stopped when the second activity is created. if The user then presses the Back button, the first activity is restarted. the user has es a phone call while using your app on his or her phone. in the lifecycle of an activity, it is very important to stop and restart your activity, this ensures that your users can feel that your app is always active, rather than losing their processes. In the following situations, your activity needs to be stopped or restarted: the user opens the "recently used application" window and switches from your app to another app. Then the activity running on the front-end of your app stops. If the user returns to your app from the Enable icon in the main menu or the "recently used application" window, the activity will be restarted. The user executes an action to start a new activity in your app. The current activity stops when the second activity is created. If you press the return button later, the first activity will be restarted. The user receives a call when using your app. The Activity class provides two lifecycle methods, onStop () and onRestart (), which allow you to specifically handle how your activity handles being stopped and restarted. unlike the paused state, which identifies a partial UI obstruction, the stopped state guarantees that the UI is no longer visible and the user's focus is in a separate activity (or an entirely separate app ). the Activity class provides two life cycle Methods: on Stop () and onRestart (), which allow you to explicitly control how your activity should be stopped and restarted. Unlike the pause status (only partial UI is blocked), the stop status ensures that the UI is no longer visible to users, in addition, the user's focus is on a single activity (or completely another independent app ). Note: Because the system retains your Activity instance in system memory when it is stopped, it's possible that you don't need to implement the onStop () and onRestart () (or even onStart () methods at all. for most activities that are relatively simple, the activity will stop and restart just fine and you might only need to use onPause () to pause ongoing actions and disconnect from system resources. note Meaning: when your Activity instance is stopped, the system retains it in the memory. You may not need to implement the onStop () and onRestart () (or even onStart () methods. For most relatively simple activities, they can be properly stopped and restarted, and you may only need to use onPause () to pause ongoing actions and separate them from system resources ). Figure 1. when the user leaves your activity, the system callonstop () to stop the activity (1 ). if the user returns while the activity is stopped, the system callonrestart () (2), quickly followed by onStart () (3) and onResume () (4 ). notice that no matter what scenario causes the activity to stop, the system always callonpause () before calling onStop (). figure 1. when a user leaves your activity, the system calls onStop () to stop activ. Ity (1 ). If the user returns again when the activity is stopped, the system calls onRestart () (2), and then calls onStart () (3) and onResume () (4 ). In either case, the system will call onPause () before onStop () to stop the activity (). Stop Your Activity -- Stop your activity When Your Activity has es a call to the onStop () method, it's no longer visible and shoshould release almost all resources that aren't needed while the user is not using it. once your activity is stopped, the system might destroy the instance if it needs to recover system memory. in extreme cases, the system might simply kill your app process without calling the activity 'S final onDestroy () callback, so it's important you use onStop () to release resources that might leak memory. when your activity receives a call to the onStop () method, it will no longer be visible and should release almost all resources that users do not need to use it. Once your activity stops, if the system needs to recycle the system memory, it may destroy the instance. In extreme cases, the system may kill your app process without calling the activity's ultimate onDestroy () callback function, because onStop () is used () to release resources that may cause memory leakage. Although the onPause () method is called before onStop (), you shocould use onStop () to perform larger, more CPU intensive shut-down operations, such as writing information to a database. although the onPause () method is called before onStop (), you should use onStop () for more intensive CPU operations, such as writing information to the database. For example, here's an implementation of onStop () that saves the contents of a draft note to persistent storage: For example, the following onStop () [java] <span style = "color: #000000;"> @ Override protected void onStop () {super. onStop (); // Always call the superclass method first // Save the note's current draft, because the activity is stopping // and we want to be sure the current note progress isn' t l Ost. contentValues values = new ContentValues (); values. put (NotePad. notes. COLUMN_NAME_NOTE, getCurrentNoteText (); values. put (NotePad. notes. COLUMN_NAME_TITLE, getCurrentNoteTitle (); getContentResolver (). update (mUri, // The URI for the note to update. values, // The map of column names and new values to apply to them. null, // No SELECT criteria are used. null // No WHERE columns are used .);} </ Span> When your activity is stopped, the Activity object is kept resident in memory and is recalled when the activity resumes. you don't 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 View in the layout, so if the user entered text into an EditText widget, that content Is retained so you don't need to save and restore it. When your activity stops, the Activity object is stored in the memory and then called again when the activity starts again. You do not need to initialize those parts created in any callback function again. The system will also track the current status of each View in the layout. Therefore, if you enter text in the EditText window, the content will be retained, so you do not need to save or reload them. Note: Even if the system destroys your activity while it's stopped, it still retains the state of the View objects (such as text in an EditText) in a Bundle (a blob of key-value pairs) and restores them if the user navigates back to the same instance of the activity (the next lesson talks more about using a Bundle to save other state data in case your activity is destroyed and recreated ). note: even in your ac When tibench is stopped, the system destroys it. The system retains the state of the View object (such as the text in an EditText) in a Bundle (Binary large object of a key-Value Pair ), if you navigate to the same activity instance system, the system will reload them (next lesson will discuss more about how to use a Bundle to save other States when your activity is destroyed and rebuilt ). Start/Restart Your Activity -- Start/Restart your activity When Your Activity comes back to the foreground from the stopped state, it calls es a call to onRestart (). the system also callthe onStart () method, which happens every time your activity becomes visible (whether being restarted or created for the first time ). the onRestart () method, however, is called only when the activity resumes from the stopped state, So you can use it to perform special restoration work that might be necessary only if the activity was previusly stopped, but not destroyed. when your activity returns from the stop status to the foreground, it receives an onRestart () call. The system will also call the onStart () method, which will occur when your activity becomes visible (whether it is restarted or created for the first time ). However, the onRestart () method is called only when the activity starts from the stopped status again, therefore, you need to use it for special recovery (only when the activity is stopped but not destroyed ). It's uncommon that an app needs to use onRestart () to restore the activity's state, so there aren't any guidelines for this method that apply to the general population of apps. however, because your onStop () method shocould essential clean up all your activity's resources, you'll need to re-instantiate them when the activity restarts. yet, you also need to instantiate them when your activity is cre Ated for the first time (when there's no existing instance of the activity ). for this reason, you shoshould usually use the onStart () callback method as the counterpart to the onStop () method, because the system callonstart () both when it creates your activity and when it restarts the activity from the stopped state. it is uncommon for an app to use onRestart () to reload the activity state. Therefore, there is no guidance for this method applied to the parent app. However, because your onStop () method should essentially clear all resources of your activity, you will need to instantiate them again when the activity restarts. However, when your activity is created for the first time (there are no existing activity instances at this time), you also need to instantiate them. For this reason, you should usually use the onStart () callback function as the corresponding version of The onStop () method, because the system will call onStart () to restart the activity when you create and stop the activity (). For example, because the user might have been away from your app for a long time before coming back it, the onStart () method is a good place to verify that required system features are enabled: for example, because the user may have been away from your app for a long time when returning, the onStart () method is an excellent place to verify system performance: [java] @ Override protected void onStart () {super. onStart (); // Always call the superclass method first // The activity is either being restarted Or started for the first time // so this is where we shoshould make sure that GPS is enabled LocationManager locationManager = (LocationManager) getSystemService (Context. LOCATION_SERVICE); boolean gpsEnabled = locationManager. isProviderEnabled (LocationManager. GPS_PROVIDER); if (! GpsEnabled) {// Create a dialog here that requests the user to enable GPS, and use an intent // with the android. provider. settings. ACTION_LOCATION_SOURCE_SETTINGS action // to take the user to the Settings screen to enable GPS when they click "OK" }}@ Override protected void onRestart () {super. onRestart (); // Always call the superclass method first // Activity being restarted from stopped state} When the system destroys your activity, it callthe onDestroy () method for your Activity. because you shoshould generally have released most of your resources with onStop (), by the time you receive a call to onDestroy (), there's not much that most apps need to do. this method is your last chance to clean out resources that cocould lead to a memory leak, so you should be sure that additional threads Are destroyed and other long-running actions like method tracing are also stopped. When the system destroys your activity, it will call the onDestroy () method for your Activity. Because you should usually release almost all resources in onStop (). When you receive an onDestroy () call, most apps do not need to do much. This method is the last chance for you to clear resources that may cause memory leakage. Therefore, you should ensure that all additional threads are destroyed and other long-term running actions, such as tracing functions, it is also stopped.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.