[Android training video series] 2.3 stopping and restarting an activity

Source: Internet
Author: User
Tags home screen

1. Main Content

This section describes how to use onstop, onrestart, and onstart. In this section, we can learn what operations should be performed in onstop and onstart.
2. Watch the video
Http://www.eyeandroid.com/thread-11346-1-1.html

3. Translation Reference

 

Stop and restart an activity

 

In the activity life cycle, it is very important to stop and restart the activity properly. This ensures that the user is aware of the existence of the program and will not lose their progress. In the following key scenarios, stopping and restarting are involved:

. The user opens the "Recent programs (recent Apps)" menu and switches from the current app to another app. At this time, the previous app is stopped. If you use the home screen launcher icon or recent apps to return to the app, the activity restarts. When you start a new activity in the current app, the current activity will be stopped after the second activity is created. If you click back, the previous activtiy will be restarted. The user receives a call when running the app.

The activity class provides the onstop () and onrestart () methods for calling when the activity is stopped or restarted. Unlike the partially blocked user interface in the paused state, the UI is invisible during the stopped state and the user's focus is transferred to another activity.

| Note: because the system saves the activity instance in the memory when the activity is stopped, the onstop () method and onrestart () method, or even onstart () method are not required in many cases () method. Because most of the activities are relatively simple, they will automatically stop and restart normally. You only need to use onpause () to stop running operations and disconnect system resources.

 

1. When the user leaves the activity, the system will call onstop () to stop the activity (1 ). At this time, if the user returns, the system will call onrestart () (2), followed by onstart () (3) and onresume () (4 ). Note that the onpause () method is always called before onstop () for whatever reason the activity is stopped.

Stop Activity

When an activity calls the onstop () method, the activity is no longer visible and should release all resources that are no longer needed. Once the activity stops, the system may destroy the instance of the activity to recycle the memory. Even, the system will directly kill your app process without executing the ondestroy () callback method of the activity, therefore, onstop () is required to release resources to avoid Memory leakage.

Although the onpause () method is called before onstop (), you should usually use onstop () to perform CPU-intensive shutdown operations, such as writing data to the database.

For example, the following is an example of saving the draft Notes to the permanent storage medium in the onstop () method:

@Overrideprotected 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 lost.    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.            );}

When the activity has stopped, the activity object will be stored in the memory and re-called when the activity is restored. You do not need to re-initialize the components that are stored in the memory before restoring to the resumed state. The system also saves the current status of each view in the layout. If you input text in the edittext component, the view is saved, so you do not need to save or restore it.

Note: even if the system will destroy the activity when the activity is stopped, the system will still save the view object (such as text in the text edit box) to a bundle, and restore the user when returning the activity (the next section describes how to use bundle to save the status of other data when the activity is destroyed and rebuilt ).

Start and restart an activity

Onrestart () is called when the activity is returned from the stopped status to the foreground. The system then calls the onstart () method. The onstart () method is called every time the activity is visible. The onrestart () method is called only when the activity is restored from the stopped state. Therefore, you can use it to perform some special restoration work. Please note that it was previously stopped rather than destrory.

It is not common to use onrestart () to restore the activity state. Therefore, there is no instruction for how to use this method. However, because the onstop () method is used to clear all activity resources, you need to re-instantiate the resources to be cleared when you restart activtiy. Likewise, You need to instantiate those resources when the activity is created for the first time. Because the system will call onstart () when creating an activity and restarting the activity from the stopped state, onstart () should be used as the method corresponding to onstop.

For example, because it may take some time before the user returns to activity, the onstart () method is a good method to verify whether certain required functions are ready.

@Overrideprotected 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 should 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"    }}@Overrideprotected void onRestart() {    super.onRestart();  // Always call the superclass method first        // Activity being restarted from stopped state    }

When the system destory is an activity, it will call the ondestroy () method for the activity. The onstop method is used to release resources, while the ondestory method is used to clear the locations that may cause memory leakage, therefore, make sure that all threads are destroy and all operations are stopped.

 
Related Article

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.