Android official Getting Started document [14] Stop and restart an activity

Source: Internet
Author: User
Tags home screen

Android official Getting Started document [14] Stop and restart an activity
Stopping and restarting an Activity
Stop and restart an activity

This lesson teaches
1.Stop Your Activity
2.start/restart Your Activity

You should also read
? Activities

This lesson teaches you
1. Stop your Activity
2. Start/Restart your activity

You should also read
? Activity activities

Try it Out

Download the Demo
Activitylifecycle.zip
Give it a try.
Download Demo
Activitylifecycle.zip

Properly stopping and restarting your activity is a important process in the activity lifecycle that ensures your users P Erceive that your app is always alive and doesn ' t lose their progress. There is 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 was stopped when the second activity was created. If the user then presses the back button, the first activity is restarted.
? The user receives a phone call and the using your app on he or her phone.
Properly stopping and restarting activity activities is an important process in the activity cycle to ensure that your users perceive that your application is always alive and will not lose their process. There are several key scenarios in which your activity stops and restarts:
? The user opens the closest window from the application and switches your application to another application. Activity activities in your application are currently stopped in the foreground. If the user returns to the Launcher icon from the home screen or the recent application window, the activity will restart your application.
? The user initiates a new activity action in your application. When you create a second activity, the current activity stops. If the user then presses the back button, the first activity event is restarted.
? The user who uses his or her phone in your application will receive a call.

the Activity class provides, lifecycle methods, OnStop () and Onrestart (), which allow Specifically handle how your activity handles being stopped and restarted. Unlike the paused state, which identifies a partial UI obstruction, the stopped state guarantees, 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, OnStop () and Onrestart (), which allow you to specifically handle your activity handlesactivity the active handle is stopped and restarted. Unlike a paused state, which determines a local UI block, the stop State guarantees that the UI is no longer visible and the user's focus is in a separate activity (or completely standalone application).

Note:because the system retains your Activity instance in system memory when it's stopped, it's possible that's you don ' t Need to implement the OnStop () and Onrestart () (or even OnStart () methods at all. For most activities that is relatively simple, the activity would stop and restart just fine and you might only need to us e OnPause () to pause ongoing actions and disconnect from system resources.
Note: Because the system retains activity instances in system memory when it stops, it may be that you do not need to implement OnStop () and Onrestart () (Even the OnStart () method at all for most activity activities. Relatively simple, activity activities will stop and restart just fine, you may just need to use the OnPause () to pause the action being taken and disconnect from the system resources.

Figure 1. When the user leaves your activity, the system calls OnStop () to the Stop the Activity (1). If the user returns while the activity was stopped, the system calls Onrestart (2), quickly followed by OnStart () (3) and Onresume () (4). Notice that no matter-scenario causes the activity to stop, the system always calls OnPause () before calling OnStop () .
Figure 1: When the user leaves your activity, the system calls the OnStop () to stop activity (1). If the activity stops the user returning, the system calls Onrestart () (2), which soon follows OnStart () (3) and Onresume () (4). Note that no matter what the situation causes activity to stop, the system () calls the OnPause () that preceded the OnStop call.

Stop Your Activity
Stop your activity.


--------------------------------------------------------------------------------

when your activity receives a call to the OnStop () method, it's no longer visible and sho Uld release almost all resources, aren ' t needed while the user was 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 is no longer visible and should be freed from almost all resources that are not needed when the user does not use it. Once your activity stops, the system may break for example if you need to restore system memory. In extreme cases, the system may simply kill your application without having to invoke the last OnDestroy () callback of that activity, allowing you to use the OnStop () to release a potentially leaking memory resource is important.

Although the OnPause () method is called before OnStop (), you should use OnStop () to perform larger, more CPU intensive Shu T-down operations, such as writing information to a database.
Although the OnPause () method is called before OnStop (), you should use the OnStop () to perform a larger, more intensive CPU shutdown operation, such as writing information to the database.

For example, here's an implementation of onStop () so saves the contents of a draft note to persistent storage:
For example, here's the implementation of OnStop (), saving a draft to the contents of a permanent store:

@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 is 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 is used.
            null    //No WHERE Columns is used.
           );
}

when your activity is stopped, the activity object was kept resident in memory and is RecA Lled when the activity resumes. You don ' t need to re-initialize-were created during any of the callback methods leading up to the resumed State. The system also keeps track of the "the" for each View in the layout, so if the user entered text to an EditText Widget, that's content is retained so you don ' t need to save and restore it.
When your activity stops, the activity object remains in memory while the activity is resumed. You do not need to reinitialize any of the callback methods in the process to cause the components that were created in the restored state. The system also tracks the current state of view in each layout, so if the user enters the text into a EditText widget, the content is retained, so you don't need to save and restore it.

Note:even if the system destroys your activity while it's stopped, it still retains the state of the View objects (such a s text in an EditText) in a Bundle (a blob of key-value pairs) and restores them if the user navigates back to the same in Stance of the activity (the next lesson talks more on using a Bundle to save other state data in case your activity are Destroyed and recreated).
Note: Even if the system destroys your activity while it's stopped, it still retains the status of a bundle (a blob of key-value pairs) to view objects (such as a edittext text) and restore them, If the user returns the same instance of the activity (the next lesson talks more about using the Bundel bundle to save your activity from being destroyed and recreating other state data).

Start/restart Your Activity
Start/Restart your activity


--------------------------------------------------------------------------------

when your activity comes back to the foreground from the stopped state, it receives a CAL L to Onrestart (). The system also calls the OnStart () method, which happens every time your activity becomes visible (whether being restarte D or created for the first time). The Onrestart () method, however, is called if the activity resumes from the stopped state, so can use it to per Form Special restoration work this might is necessary only if the activity is previously stopped, but not destroyed.
When your activity returns from the foreground of the stop state, it receives the call Onrestart (). The system also calls the OnStart () method, which happens every time your activity becomes visible (whether it is restarted or created for the first time). The Onrestart () method, however, is known to recover from the inactivity activity, only if, so you can use it to perform only in the previous activity stop, but not be destroyed, may require special repair work.

It's uncommon that a app needs to use Onrestart () to restore the activity's state, so there aren ' t any guidelines for thi s method that is apply to the general population of apps. However, because your OnStop () method should essentially-all your activity's resources, you'll need to re-instant Iate them when the activity restarts. Yet, also need to instantiate them when your activity was created for the first time (when there ' s no existing instance of the activity). For this reason, should usually use the OnStart () callback method as the counterpart to the OnStop () method, because T He system calls OnStart () both when it creates your activity and while it restarts the activity from the stopped state.
It is rare that an application needs to use Onrestart () to restore the state of the activity, so there will be no guidelines for the general population of this method applicable to the application. However, because your OnStop () method should essentially clean up all the activity's resources, you need to re-instantiate their activity when it restarts. However, it is also necessary that your activity be created for the first time to instantiate them (when there are any existing instances of activity activities). For this reason, you should usually use the OnStart () callback method as the corresponding to the OnStop () method, because the system calls OnStart () when you create activity activities, and when you restart from a stopped state activity.

For example, because the user might has been away from your app for a long time before coming back it, the OnStart () meth OD is a good place to verify the required system features is enabled:
For example, because the user may have left your application for a long time before returning, the OnStart () method is a good place to verify that the feature is enabled for the required system:

@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 the is where we should make sure the GPS is enabled
Locationmanager Locationmanager =
(Locationmanager) Getsystemservice (Context.location_service);
Boolean gpsenabled = locationmanager.isproviderenabled (Locationmanager.gps_provider);

if (!gpsenabled) {
Create A dialog here this requests the user to enable GPS, and use an intent
With the Android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS ACTION
The user to the Settings screens 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 is destroys your activity, it calls the OnDestroy () method for your activity. Because you should generally has released most of your resources with OnStop (), by the time you receive a call to Ondestr Oy (), there ' s not much the most apps need to do. This method is your the last chance to the clean out resources that could leads to a memory leak, so you should being sure that Additi Onal threads is destroyed and other long-running the actions like method tracing is also stopped.
When the system destroys your activity, it calls the OnDestroy () method for your activity activities. Since you usually should have released most of your resources for OnStop (), you get a call from OnDestroy () and not much of the most applications need to do. This approach is your last chance to clean up resources that could lead to memory leaks, so you should make sure that other threads are compromised and other long-running operations such as method tracking also stop.


Next:recreating an Activity
Next page: Recreate an activity event

This article is translated from: https://developer.android.com/training/basics/activity-lifecycle/stopping.html

Android official Getting Started document [14] Stop and restart an activity

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.