Introduction of Android.app.Activity

Source: Internet
Author: User

found that the current Android is not a lot of information, and the introduction of the activity is very small, so the official document of the introduction of Android.app.Activity translated, added some of their own understanding. If you feel that I understand the wrong, please ignore. Welcome to the email discussion.

Android.app
public class

Android.app.Activity

Java.lang.Object

Android.content.Context

Android.app.ApplicationContext viewinflate.factory

Android.app.Activity Keyevent.callback Window.callback

Activity is the only thing a user can see. Almost all activity interacts with the user, so activity is primarily responsible for creating a display window where you can use Setcontentview (View) to display your own UI. Activity is often a full-screen window in front of the user, and you can use the activity as a floating window (using the windowisfloating theme) or embed it in other activity (using Activitygroup). Here are the methods that are implemented by the two subclasses of almost all activity.

OnCreate (Bundle) This method is where the activity is initialized. Most importantly, you often need to use Setcontentview (int) here to set the layout resources used by the UI layout, and you can use Findviewbyid (int) to get the corresponding view when you need to program the components in the UI.

You can do this in OnPause () when the user leaves the activity. More importantly, any changes made by the user should be committed at that point (often submitted to ContentProvider to save the data).

If you want to use Context.startactivity () to start the activity, the activity must AndroidManifest.xml have a corresponding <activity> definition in the Initiator app package 文件中 .

The activity class is an important part of the application's overall lifecycle.

Here are the topics covered:

    1. Activity life cycle
    2. Configuration changes
    3. Start activity and get results
    4. Save Persistent state
    5. License
    6. Process life cycle
Activity life cycle

Activity in the system can be managed through an activity stack. When a new activity starts, it is first placed on top of the activity stack and before the activity--that becomes the running state is also in the activity stack, but is always stored underneath it. The activity can be returned to the foreground interface only after the new activity has exited.

All activity essentially has four states:

    • Activity is in the foreground of the screen (at the top of the activity stack), which is either active or running state.

    • The activity loses focus but is still visible (the activity overhead obscures a transparent or non-full-screen activity), and its state is paused. The activity of a paused state is completely alive (it maintains all of its state and member information and is still in the management of Window manager), but it killed when the system memory is extremely poor.

    • The activity is completely dimmed by other activity, and it enters the stopped state. It still maintains all the information about the state and the members, but he is not visible to the user, and it is often killed when memory is needed elsewhere.
    • Activity is paused or stopped, and the system needs to clean it out of memory to command its finish or simply kill its process. When it is displayed again in front of the user, it must be fully restarted and restored all the way back to its previous state.

The chart below is the activity's state diagram, and the rectangular rectangle represents the callback method, which you can implement to enable the activity to perform the actions you have made while changing the state. The colored ellipse is the main state of the activity.

Here are three more critical lifecycles.

    • From the initial call to OnCreate (Bundle) to the final call OnDestroy () is called the full life cycle . The activity will set all the "global" states in OnCreate () and release all held resources in OnDestroy (). For example, if it has a background thread that downloads data from the network, he might create the thread in OnCreate () and stop the thread in OnDestroy ().
    • Starting with the activity call OnStart (), it is called the visible life cycle until the corresponding OnStop () is called. During this time, the user can see the activity on the screen, although not necessarily in the foreground or in the user interaction. Between the two methods you can maintain the resources that the activity requires when the user is displayed. For example, you can register a intentreceiver in OnStart () to monitor changes in the environment that can affect your UI, and you can unregister the Intentreceiver in OnStop () When your UI does not continue to appear in front of the user. The OnStart () and OnStop () methods can be called multiple times throughout the life cycle whenever the activity is displayed or hidden in front of the user.
    • Starting with the activity Call Onresume (), it is called the foreground life cycle until the corresponding OnPause () is called, and this time the activity is in front of all other activity and interacts with the user. An activity can often be converted between resumed and paused states-for example, when the phone enters hibernation, when the results of the activity are returned, and when the new intent arrives-so the code in these two methods should be very brief.

The activity method below defines the full life cycle of the activity. They are all hook methods, and you can overload these methods so that the activity performs the actions you expect when the state changes. All activity should implement its own OnCreate (Bundle) method to initialize the settings, and most should implement the OnPause () method to commit the data modification and prepare to terminate the interaction with the user. Although we plan to add more tools to the system to manage the application, most of the activity still needs to implement Onfreeze () and perform the corresponding state recovery in the OnCreate (Bundle). Other methods can be implemented when needed, and it is important to note that the corresponding methods in the parent class must be called when implementing these methods.

Java code
  1. <span style="Font-size:small;" > public class Activity extends applicationcontext {
  2. protected void onCreate (Bundle icicle);
  3. protected void OnStart ();
  4. protected void Onrestart ();
  5. protected void onresume ();
  6. protected void Onfreeze (Bundle outicicle);
  7. protected void onPause ();
  8. protected void onStop ();
  9. protected void OnDestroy ();
  10. }
  11. </span>

In general, the activity's life cycle changes look more like the following table:

(Here the translator has made a large change, please refer to the original text to read the table below)


Method

Describe

Killable?

Next method

OnCreate ()

When the activity is first created, you should make a general static setting here: Create a view, bind data to a list, and so on. If there is a frozen state before the activity, then this state is provided in the bundle.

If activity is first created, OnStart () will be called after this method, and Onrestart () will be called if the activity is stopped and then re-displayed.

No

onStart() or onRestart()

OnStart ()

Called when the activity is about to be visible to the user.

Then call Onrestart () or Onresume ()(theframework makes a selective call to Onresume () is just guessing)

No

onRestart()or Onresume()

Onrestart ()

Called when activity restarts from the stopped state. Then call Onresume ().

No

onResume()

Onresume ()

This method is called when the activity is about to interact with the user, where the activity is at the top of the stack and the user input can be passed to it.

If other activity is displayed on top of it, Onfreeze () is called.

No

onFreeze()

Onfreeze ()

This method is called when your activity is paused and other activity resumes interacting with the user (before other activity shows), and you can use this method to save your current user state (typically the current instance's user state). After a pause, the system will stop (or kill) your app for the time needed to reclaim the resource supply foreground activity. Later, if your activity initiates a new instance to interact with the user, the state you save here will be passed to the new instance through the OnCreate () method.

The OnPause () method is always called thereafter.

No

onPause()

OnPause ()

When the system wants to initiate an additional activity call (before other activity displays), this method is used to commit the changes to persistent data, stop the animation, and other things that consume CPU resources. Because the next activity does not resumed before this method returns, the code executes as quickly as possible when implementing this method.

If the activity returns to the foreground, Onresume () is called and OnStop () is called if it is completely invisible to the user.

Yes

onResume()Or
onStop()

OnStop ()

Called when another activity resumes and obscures the activity, causing it to be no longer visible to the user. This scenario occurs when a new activity starts, other activity is switched to the foreground, and the current activity is destroyed.

Call Onrestart () when activity returns to the foreground and user interaction, or ondestory () if the activity exits.

Yes

onStart()Or
onDestroy()

OnDestroy ()

The last method called before your activity is destroyed, which occurs when the process terminates (call the finish () method directly to the activity or the system temporarily destroys the activity in order to save space, you can use the isfinishing () To differentiate between the two cases).

Yes

Nothing


①: This form I think there are still some debatable areas, suggested as a reference to read, whether it is the original or the translation.

Note the column "Killable" in the table above-for those methods that mark Killable, when these methods end, the activity's process may be killed at any time by the system and no longer executes any code in the activity. So you should take advantage of Onfreeze () (Save your current UI state) and OnPause () (write all the changes back to the persistent store) so that the activity can save the current state correctly when it is killed. If you need to understand the relationship between the life cycle of a process and the activity that he performs, see the Process Lifecycle section.

For those methods that mark Killable, the activity's process does not return to the system kill until these methods start until they are returned. For example, an activity is in killable state after the OnPause () method returns, and this state continues until the Onresume () method starts executing.

Configuration changes

If a device's configuration (defined in resources.configuration) changes, everything on the user interface needs to be updated to accommodate the new configuration. Because activity is the most important mechanism for interacting with users, it contains specialized support for handling configuration changes.

Unless you specify otherwise, your current activity will be destroyed when the configuration changes (such as screen orientation, language, input device, and so on), which is destroyed through a normal activity lifecycle process (Onfreeze (Bundle), OnPause (), OnStop (), and OnDestroy ()). If the activity is before the foreground, when the OnDestroy () call to the instance completes, a new instance of the activity is launched and the contents of the previous instance Onfreeze (Bundle) are passed to the new instance.

Any application resources, including layout files, may change due to any configuration value. Therefore, the only way to handle configuration changes is to regain access to all resources, including layout, drawing resources (original Drawables), and string resources. Because the activity has saved its own state and rebuilt itself from these states, it is a convenient way for the activity to restart itself to get a new configuration.

In some special cases, you may want to avoid restarting your activity when one or more configurations change. You can achieve this by setting the Android:configchanges property in manifest. You can declare here any configuration changes that the activity can handle, which will not restart the activity when these configurations change, but will invoke the activity's onconfigurationchanged (resources.configuration Method If the changed configuration contains a configuration that you cannot handle (not stated in android:configchanges), your activity will still be restarted, while the onconfigurationchanged ( Resources.configuration) will not be called.

Start activity and get results

The StartActivity (Intent) method can be used to start a new activity that will be placed at the top of the activity stack. This method has only one parameter Http://code.google.com/android/reference/android/conten

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Introduction of Android.app.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.