Stopping and restarting an activity

Source: Internet
Author: User

Stopping and restarting your activity properly is an important process in the activity lifecycle. It ensures that your users know that the app is alive and will not lose the process. In some key scenarios, your activity will be stopped and restarted:

1. the user opens the last app window and switches from your app to another app. The current activity stops at the front end of your app. If you click the icon to return to your app or return from the nearest app window, the activity will be restarted.

2. the user executes an action in your app to start a new activity. When the second activity is created, the current activity stops. If you press the back button, the first activity restarts.

3. the user receives a call when using your app

The activity class provides two life cycle Methods: onstop () and onrestart (), which allow you to handle how your activity is stopped and restarted. Unlike the paused state, the stopped State ensures that the UI is no longer visible and that the focus of the user is separated from the activity (or a completely independent APP ).

Illustration: when a user leaves your activity, the system calls onstop () (1) to stop the activity. If the user returns when your activity is in the stopped status, the system calls onrestart () (2), followed by onstart () (3) and onresume () (4 ). Note that the system always calls onpause () before onstop (), regardless of the situation that causes the activity to stop ().

 

Stop your activity

When your activity calls the onstop () method, it is no longer visible and should release almost all resources when users no longer use it. Once your activity stops, if you need to restore the system memory, the system may damage the instance. In extreme cases, the system may simply kill your app by calling the ondestroy () method, so it is important that you use onstop () to release resources, otherwise, memory leakage may occur.

Although the onpause () method is called before onstop (), you should also use onstop () for larger and more CPU-intensive shutdown operations, such as writing information to the database.

 

For example, the onstop () method is implemented to save the draft content:

@ Override

Protected VoidOnstop (){

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 =NewContentvalues ();

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.

);

}

 

 

 

 

Stopping and restarting an activity

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.